Factors of an integer: Difference between revisions
→Python: a bit faster by avoiding sets and sorting
m (→{{header|Phix}}: added some larger number examples) |
(→Python: a bit faster by avoiding sets and sorting) |
||
Line 5,577:
Much better (realize that factors come in pairs, the smaller of which is no bigger than sqrt(n)):
<syntaxhighlight lang="python">
factors1,
if n % x == 0:
x += 1
if x * x == n:
factors1.append(x)
>>> for i in (45, 53, 64): print( "%i: factors: %s" % (i, factor(i)) )▼
factors1.extend(reversed(factors2))
return factors1
for i in 45, 53, 64:
45: factors: [1, 3, 5, 9, 15, 45]
53: factors: [1, 53]
64: factors: [1, 2, 4, 8, 16, 32, 64]</
More efficient when factoring many numbers:
|