Fractran: Difference between revisions

→‎{{header|Phix}}: bugfix: prime_factors(1) now returns {1}, upped the ante
m (→‎{{header|Julia}}: global n, prmfound)
(→‎{{header|Phix}}: bugfix: prime_factors(1) now returns {1}, upped the ante)
Line 2,766:
 
<lang Phix>constant steps = 20,
primes = 2045
 
sequence known_factors = {} -- nb: no specific order
 
function combine_factors(sequence n)
-- (inverse of as_primes)
atom res = 1
for i=1 to length(n) do
if n[i]!=0 then
res *= power(known_factors[i],n[i])
end if
end for
return res
end function
 
function as_primes(integer n)
-- eg as_primes(55) -> {5,11} -> indexes to known_factors
if n=1 then return {} end if
sequence pf = prime_factors(n,duplicates:=true)
sequence res = repeat(0,length(known_factors))
Line 2,783 ⟶ 2,795:
end if
end for
-- atom chk = combine_factors(res)
-- if chk!=n then ?9/0 end if
return res
end function
Line 2,793 ⟶ 2,807:
integer {{n,d}} = sri
res[i] = {as_primes(n),as_primes(d)}
end for
return res
end function
 
function combine_factors(sequence n)
-- (inverse of as_primes)
atom res = 1
for i=1 to length(n) do
if n[i]!=0 then
res *= power(known_factors[i],n[i])
end if
end for
return res
Line 2,823 ⟶ 2,826:
if ok then
n = pgm[pc][1]
integer pad = length(n)-length(res)
if pad>0 then res &= repeat(0,pad) end if
for i=1 to length(n) do
res[i] += n[i]
Line 2,860 ⟶ 2,865:
end while
printf(1,"first %d primes: %s\n",{primes,sprint(res)})
printf(1,"%,d iterations in %s\n",{iteration,elapsed(time()-t0)})</lang>
--?get_primes(-primes)</lang>
{{out}}
<pre>
first 20 results: {15,825,725,1925,2275,425,390,330,290,770,910,170,156,132,116,308,364,68,4,30}
first 2045 primes: {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71},73,79,83,89,97,101,103,
107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197}
50752010,494,775 iterations in 08.5s4s
</pre>
For comparison with that 8.4s, I've collected Python: 386s, REXX: 60s for 25 (on tio, then it timed out), Ruby: 187s, Go: 1736s(!!), Julia 616s, FreeBasic 11.7s
 
=={{header|Prolog}}==
7,820

edits