PRIME_SERIAL is a MATLAB program which counts the number of primes between 1 and N, and is intended as a starting point for a parallel version.
The algorithm is completely naive. For each integer I, it simply checks whether any smaller J evenly divides it. The total amount of work for a given N is thus roughly proportional to 1/2*N^2.
Here are the counts of the number of primes for some selected values of N:
N | Number of Primes |
---|---|
1 | 0 |
10 | 4 |
100 | 25 |
1,000 | 168 |
10,000 | 1,229 |
100,000 | 9,592 |
1,000,000 | 78,498 |
10,000,000 | 664,579 |
100,000,000 | 5,761,455 |
1,000,000,000 | 50,847,534 |
The following results were observed for the elapsed time, running on a Macintosh PowerPC G5:
N | Pi | Time |
---|---|---|
1 | 0 | 0.000030 |
2 | 1 | 0.000016 |
4 | 2 | 0.000016 |
8 | 4 | 0.000017 |
16 | 6 | 0.000020 |
32 | 11 | 0.000030 |
64 | 18 | 0.000057 |
128 | 31 | 0.000147 |
256 | 54 | 0.000452 |
512 | 97 | 0.001548 |
1,024 | 172 | 0.005303 |
2,048 | 309 | 0.018660 |
4,096 | 564 | 0.068059 |
8,192 | 1,028 | 0.246378 |
16,384 | 1,900 | 0.914953 |
32,768 | 3,512 | 3.380086 |
65,536 | 6,542 | 12.619071 |
131,072 | 12,251 | 47.412759 |
N | Pi | Time |
---|---|---|
5 | 3 | 0.000095 |
50 | 15 | 0.000123 |
500 | 95 | 0.003194 |
5000 | 669 | 0.170837 |
50,000 | 5,133 | 13.081281 |
500,000 | 41,538 | 1,076.868061 |
total = prime ( n )where
The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.
PRIME_SERIAL is available in a C version and a C++ version and a FORTRAN77 version and a FORTRAN90 version and a MATLAB version.
COLLATZ, a MATLAB library which computes and analyzes the Collatz sequence (or "hailstone" sequence or "3n+1 sequence");
FFT_SERIAL, a MATLAB program which demonstrates the computation of a Fast Fourier Transform, and is intended as a starting point for implementing a parallel version.
MD, a MATLAB program which carries out a molecular dynamics simulation, and is intended as a starting point for implementing a parallel version.
MXM, a MATLAB program which sets up a matrix multiplication problem A=B*C, intended as a starting point for implementing a parallel version.
POISSON_SERIAL, a MATLAB program which computes an approximate solution to the Poisson equation in a rectangle, and is intended as the starting point for the creation of a parallel version.
PRIME_PARFOR, a MATLAB program which counts the number of primes between 1 and N; it runs in parallel using MATLAB's "parfor" facility.
PRIME_PLOT a MATLAB program which displays a box plot of the prime and composite numbers.
PRIME_SPMD, a MATLAB program which counts the number of primes between 1 and N; running in parallel using MATLAB's "SPMD" feature.
QUAD_SERIAL, a MATLAB program which approximates an integral using a quadrature rule, and is intended as a starting point for parallelization exercises.
QUAD2D_SERIAL, a MATLAB program which approximates an integral over a 2D region using a product quadrature rule, and is intended as a starting point for parallelization exercises.
SATISFY, a MATLAB program which demonstrates, for a particular circuit, an exhaustive search for solutions of the circuit satisfiability problem.
TIMER, MATLAB programs which demonstrate how to compute CPU time or elapsed time.
You can go up one level to the MATLAB source codes.