RosettaCodeData/Task/Circular-primes/Phix/circular-primes-1.phix
2026-02-01 16:33:20 -08:00

33 lines
802 B
Text

with javascript_semantics
function circular(integer p)
integer len = length(sprintf("%d",p)),
pow = power(10,len-1),
p0 = p
for i=1 to len-1 do
p = pow*remainder(p,10)+floor(p/10)
if p<p0 or not is_prime(p) then return false end if
end for
return true
end function
sequence c = {}
integer n = 1
while length(c)<19 do
integer p = get_prime(n)
if circular(p) then c &= p end if
n += 1
end while
printf(1,"The first 19 circular primes are:\n%v\n\n",{c})
include mpfr.e
c = {}
n = 7
mpz z = mpz_init()
while length(c)<4 do
mpz_set_str(z,repeat('1',n))
if mpz_prime(z) then
c = append(c,sprintf("R(%d)",n))
end if
n += 1
end while
printf(1,"The next 4 circular primes, in repunit format, are:\n%s\n\n",{join(c)})