Changes

Jump to navigation Jump to search
943 bytes added ,  16:28, January 10, 2008
error testing primality of 4
== Primality testing - Problems with the number 4 ==

When testing whether a number ''N'' is divisible by ''s'', using values of ''s'' from 2 to ''N'' - 1, when ''N'' is 4, the method correctly determines that 4 is not prime, because it is divisible by 2. This example is in the C programming language.
<blockquote><pre>
for (s = 2; s < N - 1; ++s) {
if (N % s == 0) {
printf("N is not a prime number.");
}
}
</pre></blockquote>

However, when using values of ''m'' from 2 to the square root of ''n'', when ''n'' is 4, an error can occur. Since 2 is the square root of 4, the test range is 2 to 2, and, if the range is exclusive, as in a for-loop, the whole test is skipped, and 4 is incorrectly determined to be a prime number.
<blockquote><pre>
for (s = 2; s < sqrt(N); ++s) {
if (N % s == 0) {
printf("N is not a prime number.");
}
}
</pre></blockquote>

[[User:Isaac4given|Isaac4given]] 11:28, 10 January 2008 (EST)

Navigation menu