The name of your C program file must be called fibdense.c, files with any other name will not be marked.
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...
The first two numbers are one. Each subsequent number is simply the sum of the immediate previous two numbers. Any number in the above infinite sequence is called a Fibonacci number.
Suppose we are given a range of integers from one to 100 inclusive. How many of those integers are Fibonacci numbers? This is known as the density of Fibs and we convert that to percentage.
There are two ways to calculate the density of Fibs in a certain range. We can do it precisely by testing if each of the numbers in the range is a Fibonacci number. This is the most precise method, but it is also the slowest.
The other method is by sampling. We take x random integers in the range and see how many of those are Fibonacci numbers. This is a faster method, but the density calculate is only an approximation.
We will implement both methods in our program.
23129 100 10000 1000The explanation for each number is given as follows:
23129 | Random number seed |
100 | Lower limit of range (inclusive) |
10000 | Upper limit of range (inclusive) |
1000 | Number of samples (x) |
The random number seed should be initialized to the value shown (by using the srand function) at the beginning of each experiment.
Note that there is no relationship between the range and the number of samples.
32251 1 100 100 32252 1 1000 1000 32253 1 10000 10000 32254 1 100000 100000 32255 1 1000000 1000000
We will test your program with other experiment data, so your program must work for the general case, and not just only for the sample input file given.
You are reminded to follow the sample outcome exactly, else marks will be deducted.
Seed: 32251, lower: 1, upper: 100, sampled fibs: xx, samples: 100, percentage: xx.xxxxxx% num fibs: xx, total: 100, percentage: xx.xxxxxx% Seed: 32252, lower: 1, upper: 1000, sampled fibs: xxx, samples: 1000, percentage: xx.xxxxxx% num fibs: xxx, total: 1000, percentage: xx.xxxxxx% Seed: 32253, lower: 1, upper: 10000, sampled fibs: xxxx, samples: 10000, percentage: xx.xxxxxx% num fibs: xxxx, total: 10000, percentage: xx.xxxxxx% Seed: 32254, lower: 1, upper: 100000, sampled fibs: xxxxx, samples: 100000, percentage: xx.xxxxxx% num fibs: xxxxx, total: 100000, percentage: xx.xxxxxx% Seed: 32255, lower: 1, upper: 1000000, sampled fibs: xxxxxx, samples: 1000000, percentage: xx.xxxxxx% num fibs: xxxxxx, total: 1000000, percentage: xx.xxxxxx%
/*---------------------------------------------------*/ /* This function generates a random integer */ /* between specified limits a and b (a<b). */ int rand_int2(int a, int b) { if (RAND_MAX == 32767) return (rand()<<15^rand())%(b-a+1) + a; else return rand()%(b-a+1) + a; } /*---------------------------------------------------*/
/*---------------------------------------------------*/ /* This function generates a random integer */ /* between specified limits a and b (a<b). */ int rand_int(int a, int b) { return rand()%(b-a+1) + a; } /*---------------------------------------------------*/
A total of 21 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 9 times.
If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.