Claus Witt dot com

webdeveloper

The seventh problem of Project Euler is again a simple problem, which can be solved by iteration. The challenge is how to establish when a number is a prime. However, reading the definition of a prime number, you can get some help. "A natural number is called a prime number (or a prime) if it is bigger than one and has no divisors other than 1 and itself." - Wikipedia. By knowing this we have a simple routine that exits early for all even numbers above 2 (which is the only even number that is a prime). After that all numbers up to the square root is checked as a divisor, if one is found, the number is not a prime.

The reason for checking only up to the square root of number, is that if any divisor exists above the square root, one must exist below the square root as well, meaning that we already have established that the number is not a prime.

The code for the problem is run with nodes… Again..

blog comments powered by Disqus