Anonymous user
Primality by trial division: Difference between revisions
{{out}}
({{out}}) |
|||
Line 1:
{{task|Prime Numbers}}Write a boolean function that tells whether a given integer is prime. Remember that 1 and all non-positive numbers are not prime.
Use trial division. Even numbers over two may be eliminated right away.
A loop from <span style="font-family:serif">3</span> to <span style="font-family:serif">√n</span> will suffice, but other loops are allowed.
* Related tasks: [[Sequence of primes by Trial Division]], [[Sieve of Eratosthenes]], [[Prime decomposition]], [[AKS test for primes]].
Line 303 ⟶ 304:
NEXT
= TRUE</lang>
{{out}}
<pre>
2 is prime
Line 817 ⟶ 818:
PAUSE
</lang>
{{out}}
<pre>2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
Press any key to continue...</pre>
Line 1,877 ⟶ 1,878:
/*Note: // is modulus. */
return 1 /*done dividing, it's prime.*/</lang>
<pre style="height:20ex">
2 is prime.
Line 2,376 ⟶ 2,377:
until Num = 0</lang>
{{out}}
<pre>
777777777
|