RosettaCodeData/Task/Prime-triangle/Phix/prime-triangle.phix
2026-02-01 16:33:20 -08:00

52 lines
1.4 KiB
Text

with javascript_semantics
atom t0 = time()
sequence can_follow, arrang
bool bFirst = true
function ptrs(integer res, n, done)
-- prime triangle recursive sub-procedure
-- on entry, arrang[done] is set and arrang[$]==n.
-- find something/everything that fits between them.
integer ad = arrang[done]
if n-done<=1 then
if can_follow[ad][n] then
if bFirst then
printf(1,"%s\n",join(arrang,fmt:="%d"))
bFirst = false
end if
res += 1
end if
else
done += 1
-- as per talk page, we only need to examine odd
-- numbers following an even number & vice versa
for i=done to n-1 by 2 do
integer ai = arrang[i]
if can_follow[ad][ai] then
integer aid = arrang[done]
arrang[i] = aid
arrang[done] = ai
res = ptrs(res,n,done)
arrang[i] = ai
arrang[done] = aid
end if
end for
end if
return res
end function
function prime_triangle(integer n)
can_follow = repeat(repeat(false,n),n)
for i=1 to n do
for j=1 to n do
can_follow[i][j] = is_prime(i+j)
end for
end for
arrang = tagset(n)
bFirst = true
return ptrs(0,n,1)
end function
sequence res = apply(tagset(20,2),prime_triangle)
printf(1,"%s\n",join(res,fmt:="%d"))
?elapsed(time()-t0)