Factors of an integer: Difference between revisions
Content added Content deleted
(→{{header|BQN}}: Version based on prime factorization) |
(→{{header|Sather}}: Use an iterator for factor generation (sadly means factors aren't ordered, but avoids allocation)) |
||
Line 5,996: | Line 5,996: | ||
=={{header|Sather}}== |
=={{header|Sather}}== |
||
{{trans|C++}} |
|||
<lang sather>class MAIN is |
<lang sather>class MAIN is |
||
factors(n :INT): |
factors!(n :INT):INT is |
||
yield 1; |
|||
⚫ | |||
f := f.append(|1|); |
|||
f := f.append(|n|); |
|||
loop i ::= 2.upto!( n.flt.sqrt.int ); |
loop i ::= 2.upto!( n.flt.sqrt.int ); |
||
if n%i = 0 then |
if n%i = 0 then |
||
yield i; |
|||
if (i*i) /= n then |
|||
yield n / i; |
|||
⚫ | |||
end; |
end; |
||
end; |
end; |
||
yield n; |
|||
return f; |
|||
end; |
end; |
||
Line 6,018: | Line 6,016: | ||
loop l ::= a.elt!; |
loop l ::= a.elt!; |
||
#OUT + "factors of " + l + ": "; |
#OUT + "factors of " + l + ": "; |
||
loop ri ::= factors!(l); |
|||
loop ri ::= r.elt!; |
|||
#OUT + ri + " "; |
#OUT + ri + " "; |
||
end; |
end; |
||
Line 6,025: | Line 6,022: | ||
end; |
end; |
||
end; |
end; |
||
end; |
end; |
||
</lang> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |