Factors of an integer: Difference between revisions

Content added Content deleted
(→‎{{header|Sather}}: Use an iterator for factor generation (sadly means factors aren't ordered, but avoids allocation))
(Add CLU)
Line 1,655: Line 1,655:
(for [x (range 1 (inc (Math/sqrt n))) :when (zero? (rem n x))]
(for [x (range 1 (inc (Math/sqrt n))) :when (zero? (rem n x))]
[x (/ n x)]))))</lang>
[x (/ n x)]))))</lang>

=={{header|CLU}}==
{{trans|Sather}}
<lang clu>isqrt = proc (s: int) returns (int)
x0: int := s/2
if x0=0 then return(s) end
x1: int := (x0 + s/x0)/2
while x1<x0 do
x0, x1 := x1, (x1 + s/x1)/2
end
return(x0)
end isqrt

factors = iter (n: int) yields (int)
yield(1)
for i: int in int$from_to(2,isqrt(n)) do
if n//i=0 then
yield(i)
if i*i ~= n then yield(n/i) end
end
end
yield(n)
end factors

start_up = proc ()
po: stream := stream$primary_output()
a: array[int] := array[int]$[3135, 45, 64, 53, 45, 81]
for n: int in array[int]$elements(a) do
stream$puts(po, "Factors of " || int$unparse(n) || ":")
for f: int in factors(n) do
stream$puts(po, " " || int$unparse(f))
end
stream$putl(po, "")
end
end start_up</lang>
{{out}}
<pre>Factors of 3135: 1 3 1045 5 627 11 285 15 209 19 165 33 95 55 57 3135
Factors of 45: 1 3 15 5 9 45
Factors of 64: 1 2 32 4 16 8 64
Factors of 53: 1 53
Factors of 45: 1 3 15 5 9 45
Factors of 81: 1 3 27 9 81</pre>


=={{header|COBOL}}==
=={{header|COBOL}}==