Prime Numbers

Summary

This page describes a simple algorithm for calculating prime numbers.  This can be applied in any language.

Prime number is a number that is greater than 1 and divided by 1 or itself. In other words, prime numbers can't be divided by other numbers than itself or 1. For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers.

Note: 0 and 1 are not prime numbers. The 2 is the only even prime number because all the other even numbers can be divided by 2.

  1. Set the lower and upper limits of the range to be covered
  2. Loop from the lower limit to the upper limit, the current point in this loop represents the number being tested
    1. test if the number being currently tested is equal to 0 or 1
      1. if it is then it is not a prime number
    2. determine the midway point of the number being tested - splitting the number to be tested into half its size reduce the amount of checking to be done, anything after its midway point is a replication of the what would be done before the midway point
    3. loop from 2 to the midway point, the number in this loop represents the modulo test number
    4. begin 
      1. perform a modulo division of the number being tested against the modulo test number
      2. if the modulo test equals zero, the number is NOT a primer number, exit the loop, otherwise ignore the result and move onto modulo test number
    5. end loop
    6. if the modulo test on 2.d.i s NOT equal to zero, the number is a primer number
  3. End loop