Factors of an integer: Difference between revisions

(→‎optimized version: added better formatting, # of divisors, PRIME indicator, changed comments. -- ~~~~)
Line 2,185:
[1, 53]
[1, 2, 4, 8, 16, 32, 64]</pre>
 
 
=={{header|Run BASIC}}==
<lang runbasic>PRINT "Factors of 45 are ";factorlist$(45)
PRINT "Factors of 12345 are "; factorlist$(12345)
END
function factorlist$(f)
DIM L(100)
FOR i = 1 TO SQR(f)
IF (f MOD i) = 0 THEN
L(c) = i
c = c + 1
IF (f <> i^2) THEN
L(c) = (f / i)
c = c + 1
END IF
END IF
NEXT i
s = 1
while s = 1
s = 0
for i = 0 to c-1
if L(i) > L(i+1) and L(i+1) <> 0 then
t = L(i)
L(i) = L(i+1)
L(i+1) = t
s = 1
end if
next i
wend
FOR i = 0 TO c-1
factorlist$ = factorlist$ + STR$(L(i)) + ", "
NEXT
end function</lang>
output
<pre>Factors of 45 are 1, 3, 5, 9, 15, 45,
Factors of 12345 are 1, 3, 5, 15, 823, 2469, 4115, 12345, </pre>
 
=={{header|Sather}}==
Anonymous user