Count in factors: Difference between revisions

Content added Content deleted
(→‎{{header|Wren}}: Now uses Wren-math module.)
(→‎{{header|Phix}}: use new builtins)
Line 2,737: Line 2,737:


=={{header|Phix}}==
=={{header|Phix}}==
Requires 0.8.2+
<lang Phix>function factorise(atom n)
<lang Phix>procedure factorise(integer n)
-- returns a list of all integer factors of n, that when multiplied together equal n
sequence res = prime_factors(n,true)
-- (adapted from the standard builtin factors(), which does not return duplicates)
res = join(apply(res,sprint)," x ")
sequence res = {}
printf(1,"%2d: %s\n",{n,res})
integer p = 2,
end procedure
step = 1,
lim = floor(sqrt(n))

while p<=lim do
while remainder(n,p)=0 do
res = append(res,sprintf("%d",p))
n = n/p
if n=p then exit end if
lim = floor(sqrt(n))
end while
p += step
step = 2
end while
return join(append(res,sprintf("%d",n))," x ")
end function

for i=1 to 10 do
for i=1 to 10 do
printf(1,"%2d: %s\n",{i,factorise(i)})
factorise(i)
end for</lang>
end for
factorise(2144)
factorise(1000000000)</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,773: Line 2,761:
9: 3 x 3
9: 3 x 3
10: 2 x 5
10: 2 x 5
2144: 2 x 2 x 2 x 2 x 2 x 67
1000000000: 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5 x 5
</pre>
</pre>