PRIME_SERIAL
Program to Count Primes


PRIME_SERIAL is a C++ 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:
NNumber of Primes
10
104
10025
1,000168
10,0001,229
100,0009,592
1,000,00078,498
10,000,000664,579
100,000,0005,761,455
1,000,000,00050,847,534

The following results were observed for the elapsed time.
NPiTime
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
1024 172 0.005303
2048 309 0.018660
4096 564 0.068059
8192 1028 0.246378
16384 1900 0.914953
32768 3512 3.380086
65536 6542 12.619071
131072 12251 47.412759

Licensing:

The computer code and data files described and made available on this web page are distributed under the GNU LGPL license.

Languages:

PRIME_SERIAL is available in a C version and a C++ version and a FORTRAN77 version and a FORTRAN90 version and a MATLAB version.

Related Data and Programs:

COLLATZ, a MATLAB library which computes and analyzes the Collatz sequence (or "hailstone" sequence or "3n+1 sequence");

FFT_SERIAL, a C++ program which demonstrates the computation of a Fast Fourier Transform, and is intended as a starting point for implementing a parallel version.

FIRE_SERIAL, a C++ program which simulates a forest fire over a rectangular array of trees, starting at a single random location. It is intended as a starting point for the development of a parallel version.

MD, a C++ program which carries out a molecular dynamics simulation, and is intended as a starting point for implementing a parallel version.

MXM_SERIAL, a C++ program which sets up a matrix multiplication problem A=B*C, intended as a starting point for implementing a parallel version.

POISSON_SERIAL, a C++ 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_MPI, a C++ program which counts the number of primes between 1 and N, using MPI for parallel execution.

PRIME_OPENMP, a C++ program which counts the number of primes between 1 and N, using OpenMP for parallel execution.

QUAD_SERIAL, a C++ program which approximates an integral using a quadrature rule, and is intended as a starting point for parallelization exercises.

QUAD2D_SERIAL, a C++ 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 C++ program which demonstrates, for a particular circuit, an exhaustive search for solutions of the circuit satisfiability problem.

SEARCH_SERIAL, a C++ program which searches the integers from A to B for a value J such that F(J) = C. this version of the program is intended as a starting point for a parallel approach.

SUBSET_SUM_SERIAL, a C++ program which seeks solutions of the subset sum problem, in which it is desired to find a subset of a set of integers which has a given sum; this version of the program is intended as a starting point for a parallel approach.

Reference:

  1. Eratosthenes,
    A Method For Finding Prime Numbers,
    Papyrus 487,
    Library of Alexandria.

Source Code:

Examples and Tests:

List of Routines:

You can go up one level to the C++ source codes.


Last revised on 09 November 2011.