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.
- Set the lower and upper limits of the range to be covered
- Loop from the lower limit to the upper limit, the current point in this loop represents the number being tested
- test if the number being currently tested is equal to 0 or 1
- if it is then it is not a prime number
- 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
- loop from 2 to the midway point, the number in this loop represents the modulo test number
- begin
- perform a modulo division of the number being tested against the modulo test number
- 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
- end loop
- if the modulo test on 2.d.i s NOT equal to zero, the number is a primer number
- test if the number being currently tested is equal to 0 or 1
- End loop