CS1101C Lab 3 (Odd Week)

Prime Number Density

The deadline for this lab question is Friday 20 October 2006, 23:59:59 hours.

The name of your C program file must be called density.c, files with any other name will not be marked.

Background

We know that a prime number is an integer >= 2 that is only divisible by one and itself. Suppose we are given a range of integers from two to 100 inclusive. How many of those integers are prime numbers? This is known as the density of primes and we convert that to percentage.

There are two ways to calculate the density of primes in a certain range. We can do it precisely by testing if each of the numbers in the range is a prime 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 prime. This is a faster method, but the density calculate is only an approximation.

We will implement both methods in our program.

Implementation

You will be provided with an input file consisting one or more lines. Each line contains the parameters for each experiment. Each line consists of a random number seed, the lower limit of the range, the upper limit of the range, and the number of samples. A typical line from the input file consist of four integers separated by blank spaces as shown below:
23129 100 10000 1000
The explanation for each number is given as follows:

23129Random number seed
100Lower limit of range (inclusive)
10000Upper limit of range (inclusive)
1000Number 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.

Input File

Assume that the text input file is always called primes1.txt. This sample input file contains data for five experiments:

23130 2 100 100
23131 2 1000 1000
23132 2 10000 10000
23133 2 100000 100000
23134 2 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.

Sample Run

The following shows a sample run for the input file primes1.txt. Actual values for the experiment results have been replaced with x’s (the number of x’s does not signify the number of digits). Display the percentage to three decimal places. To print a % sign in a printf statement, use %%.

You are reminded to follow the sample outcome exactly, else marks will be deducted.

Seed: 23130, lower: 2, upper: 100,
      sampled primes: xx, samples: 100, percentage: xx.xxx%
      num primes: xx, total: 99, percentage: xx.xxx%
Seed: 23131, lower: 2, upper: 1000,
      sampled primes: xxx, samples: 1000, percentage: xx.xxx%
      num primes: xxx, total: 999, percentage: xx.xxx%
Seed: 23132, lower: 2, upper: 10000,
      sampled primes: xxxx, samples: 10000, percentage: xx.xxx%
      num primes: xxxx, total: 9999, percentage: xx.xxx%
Seed: 23133, lower: 2, upper: 100000,
      sampled primes: xxxxx, samples: 100000, percentage: xx.xxx%
      num primes: xxxxx, total: 99999, percentage: xx.xxx%
Seed: 23134, lower: 2, upper: 1000000,
      sampled primes: xxxxxx, samples: 1000000, percentage: xx.xxx%
      num primes: xxxxxx, total: 999999, percentage: xx.xxx%

Take Note

Tips

Ponder

Is your sampling accurate? How can you ensure higher sampling accuracy?

Slightly Advanced Pondering

If your sampling is accurate, what does it indicate about the numbers generated by the random number generator?

More Advanced Pondering

When compiling and executing the program in sunfire, try using the following rand_int function instead. Does it work? Why or why not?
/*---------------------------------------------------*/
/*  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;
}
/*---------------------------------------------------*/

Super Advanced Pondering

Write your own Pseudo Random Number Generator (PRNG).

Ultra Advanced Pondering

Write your own True Random Number Generator (TRNG) based on real life random data. Test the quality of your random numbers generated by your RNG. Google is your friend.


This document, index.html, has been accessed 21 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 13 different hosts have accessed this document in the last 445 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 6 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.