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:
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.
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
- Assume that the text input file always exists and contains only
valid integers.
- Assume that all the numbers in the input file will never be greater
than 1,000,000 or smaller than 2.
- Do not use any arrays in your program.
Tips
- To print a % sign in a printf statement, use %%.
- Use the following rand_int2 function to generate random
numbers.
/*---------------------------------------------------*/
/* 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;
}
/*---------------------------------------------------*/
- If you have not yet read the article How
NOT to Go About a Programming Assignment (which was found in the
IVLE workbin), read it now.
- This may be the most complicated program you have ever designed. Go
to the drawing board and plan your program. Think about the individual
modules you want using the divide-and-conquer strategy. Write and test
each module / function individually. Do not ever write the complete
program, and then start compiling / running / testing it. You will end
up with numerous compilation and logic errors which will take a long
time to fix.
- You should probably not use the input file given when testing
your program for the first time; you should create your own input file
(by using the Unix command “vim primes1.txt”) with
manageable values and small number of experiments. When you are
confident that your program is working for small number of experiments,
increase it.
- You should use many printf statements to give more
information of what your program is doing exactly. You should print out
as much debugging information as you need; then before submission,
comment out the debugging information.
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.