Primality by trial division: Difference between revisions
Content added Content deleted
Langurmonkey (talk | contribs) |
Langurmonkey (talk | contribs) |
||
Line 3,040: | Line 3,040: | ||
following the Raku example, which states, "Integer $i is prime if it is greater than one and is divisible by none of 2, 3, up to the square root of $i" (plus an adjustment for the prime number 2) |
following the Raku example, which states, "Integer $i is prime if it is greater than one and is divisible by none of 2, 3, up to the square root of $i" (plus an adjustment for the prime number 2) |
||
⚫ | |||
Below, we use an implied parameter (.i) on the .isPrime function. |
|||
val isPrime = fn(i) { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
} |
} |
||
writeln filter |
writeln filter(isPrime, series(100)) |
||
</syntaxhighlight> |
|||
=== Recursive === |
=== Recursive === |
||
{{trans|Go}} |
{{trans|Go}} |
||
<syntaxhighlight lang="langur"> |
<syntaxhighlight lang="langur"> |
||
val isPrime = fn(i) { |
|||
val n = abs(i) |
|||
if n <= 2: return n == 2 |
|||
val |
val chkdiv = fn(n, i) { |
||
if |
if i * i <= n { |
||
return |
return n ndiv i and self(n, i+2) |
||
} |
} |
||
return true |
return true |
||
} |
} |
||
return |
return n ndiv 2 and chkdiv(n, 3) |
||
} |
} |
||
writeln filter |
writeln filter(isPrime, series(100)) |
||
</syntaxhighlight> |
|||
{{out}} |
{{out}} |