Count in factors: Difference between revisions

→‎{{header|Phix}}: use new builtins
(→‎{{header|Wren}}: Now uses Wren-math module.)
(→‎{{header|Phix}}: use new builtins)
Line 2,737:
 
=={{header|Phix}}==
Requires 0.8.2+
<lang Phix>functionprocedure factorise(atominteger 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)
returnres = join(appendapply(res,sprintf("%d",n)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
printf(1,"%2d: %s\n",{i,factorise(i)})
end for</lang>
factorise(2144)
factorise(1000000000)</lang>
{{out}}
<pre>
Line 2,773 ⟶ 2,761:
9: 3 x 3
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>
 
7,805

edits