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):ARRAY{INT} is
factors!(n :INT):INT is
f:ARRAY{INT};
yield 1;
f := #;
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
f := f.append(|i|);
yield i;
if (i*i) /= n then f := f.append(|n / i|); end;
if (i*i) /= n then
yield n / i;
end;
end;
end;
end;
end;
f.sort;
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 + ": ";
r ::= factors(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;</lang>
end;
</lang>


=={{header|Scala}}==
=={{header|Scala}}==