Factors of an integer: Difference between revisions

→‎{{header|Python}}: Now that someone mentioned efficiency...
(→‎{{header|Python}}: Write out python version in a readable and more efficient way. Use of sum() in the old one was wasteful and confusing.)
(→‎{{header|Python}}: Now that someone mentioned efficiency...)
Line 1,467:
53: factors: [1, 53]
64: factors: [1, 2, 4, 8, 16, 32, 64]</lang>
 
More efficient when factoring many numbers:<lang python>def factor(n):
a, r = 1, [1]
while a * a < n:
a += 1
if n % a: continue
b, f = 1, []
while n % a == 0:
n //= a
b *= a
f += [i * b for i in r]
r += f
if n > 1: r += [i * n for i in r]
return r</lang>
 
=={{header|R}}==
Anonymous user