Project Euler :: Algorithms
- 1 is not a prime.
- All primes except 2 are odd.
- All primes greater than 3 can be written in the form
$6k+/-1$ . - Any number n can have only one prime factor greater than sqrt(n) .
- The consequence for primality testing of a number n is: if we cannot find a number f less than or equal
$\sqrt{n}$ that divides$n$ then$n$ is prime: the only prime factor of$n$ is$n$ itself.
function isPrime(n)
if n=1 then return false
else
if n<4 then return true //2 and 3 are prime
else
if n mod 2=0 then return false
else
if n<9 then return true //we have already excluded 4,6 and 8.
else
if n mod 3=0 then return false
else
r=floor( sqrt( n ) ) // sqrt(n) rounded to the greatest integer r so that r*r<=n
f=5
while f<=r
if n mod f=0 then return false (and step out of the function)
if n mod(f+2)=0 then return false (and step out of the function)
f=f+6
end while
return true (in all other cases)
End Function
The basic idea behind this ancient method is that instead of looking for divisors d of n, we mark multiples of d as composites. Since every composite has a prime divisor, the marking of multiples need only be done for primes. The classical algorithm is:
-
Make a list of all numbers from 2 to N.
-
Find the next number p not yet crossed out. This is a prime. If it is greater than
$\sqrt{N}$ go to 5. -
Cross out all multiples of p which are not yet crossed out.
-
Go to 2.
-
The numbers not crossed out are the primes not exceeding
$N$ .
You only need to start crossing out multiples at
Every number
Triangle number:
Any integer N can be expressed as follows:
$N = p_1^{a_1}*p_2^{a_2}p_3^{a_3}...$
where
For example,
Furthermore, the number of divisors
For example, the number of divisors of 28 would be: $D(28) = (2+1)(1+1) = 32 = 6$ A table of primes will be required to apply this relationship. The efficient preparation of a prime table is already covered in the overview for Problem 7 and will not be discussed here. Since the largest expected triangle number is within a 32-bit integer, a table containing primes up to 65500.
1 1 1 1
1 2 3 4
1 3 6 10
1 4 10 20