Skip to content

Commit 8dc5c8d

Browse files
committed
speedup factorization for numbers with many small factors
1 parent 485ab53 commit 8dc5c8d

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

pythagorean_tuples.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from itertools import product
1+
from itertools import product, count
22
from math import log, isqrt, ceil, prod
33
from collections import Counter
44

@@ -49,7 +49,10 @@ def _prime_factors(n: int) -> list:
4949
while n % 2 == 0:
5050
factors.append(2)
5151
n //= 2
52-
for i in range(3, isqrt(n) + 1, 2):
52+
for i in count(3, 2):
53+
if i ** 2 > n:
54+
break
55+
5356
while n % i == 0:
5457
factors.append(i)
5558
n //= i

0 commit comments

Comments
 (0)