Fractran: Difference between revisions

m
added some comments
m (added some comments)
Line 2,546:
=={{header|Phix}}==
Using the same ideas as the Fortran entry (thanks!).
 
For example, suppose that known_factors happens to be {2,3,5}. [the exact order may vary]<br>
2 is held as {1,0,0}, ie 2^1 * 3^0 * 5^0, and 15 as {0,1,1}, ie 2^0 * 3^1 * 5^1.
 
Notice that 2, being a power of 2, has zero in all slots other than 2.<br>
We can say that 15 is not exactly divisible by 2 because the power of 2 is too large.<br>
Division (to whole integer) is performed simply by subtracting the corresponding powers.
 
<lang Phix>constant steps = 20,
primes = 20
Line 2,579 ⟶ 2,587:
 
function combine_factors(sequence n)
-- (inverse of as_primes)
atom res = 1
for i=1 to length(n) do
Line 2,633 ⟶ 2,642:
if n=0 then exit end if
n0[k2] = n[k2]
if n=n0 then -- (ie all non-2 are 0)
-- and the prime itself is ready and waiting...
res = append(res,n[k2])
end if
7,820

edits