Jump to content

Primality by trial division: Difference between revisions

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)
 
<syntaxhighlight lang="langur">val .isPrime = fn(.i) {
Below, we use an implied parameter (.i) on the .isPrime function.
val isPrime = fn(i) {
 
.i == 2 or .i > 2 and
<syntaxhighlight lang="langur">val .isPrime = fn(.i) {
not any(fn fn(.x) { .:i div .x }, pseries (2 .. .i ^/ 2))
.i == 2 or .i > 2 and
not any fn(.x) { .i div .x }, pseries 2 .. .i ^/ 2
}
 
writeln filter .(isPrime, series (100</syntaxhighlight>))
</syntaxhighlight>
 
=== Recursive ===
{{trans|Go}}
<syntaxhighlight lang="langur">val .isPrime = fn(.i) {
val .nisPrime = absfn(.i) {
ifval .n <= 2: return .n == 2abs(i)
if n <= 2: return n == 2
 
val .chkdiv = fn(.n, .i) {
if .i * .i <= .n {
return .n ndiv .i and self(.n, .i+2)
}
return true
}
 
return .n ndiv 2 and .chkdiv(.n, 3)
}
 
writeln filter .(isPrime, series (100</syntaxhighlight>))
</syntaxhighlight>
 
{{out}}
990

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.