45 lines
1.1 KiB
Text
45 lines
1.1 KiB
Text
link fastprime
|
|
|
|
procedure main(A)
|
|
limit := \A[1] | 19
|
|
write("First ",limit," circular primes are:")
|
|
every writes(" ",(x := circular(genprimes())\limit)|"\n")
|
|
|
|
limit := 3 # Patience wore out when limit = 4...
|
|
writes("Next ",limit," are:")
|
|
y := repunit(*x)
|
|
cnt := 0
|
|
while cnt < limit do {
|
|
if (circular(is_prime(y)),cnt +:= 1) then writes(" R(",*y,")")
|
|
y := 10*y+1
|
|
}
|
|
write()
|
|
|
|
limit := 1 # Patience wore out when limit > 1...
|
|
every x := (5003|9887|15073|25031|35317|49081)\limit do {
|
|
writes("R(",x,") ")
|
|
y := repunit(x)
|
|
write("is ",if is_prime(y) then "prime" else "not prime")
|
|
}
|
|
end
|
|
|
|
procedure repunit(x)
|
|
every (y := 0, 1 to x, y := 10*y+1)
|
|
return y
|
|
end
|
|
|
|
procedure circular(n)
|
|
static seen
|
|
initial seen := set()
|
|
if member(seen,n) then fail
|
|
m := c := n
|
|
x := *n
|
|
every i := 2 to x do {
|
|
if (d := c%10) = 0 then fail
|
|
c := (d*10^(x-1))+(c/10)
|
|
if not is_prime(numeric(c)) then fail
|
|
if member(seen,m >:= c) then fail
|
|
}
|
|
insert(seen,m)
|
|
return n
|
|
end
|