We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 94f38dd commit 1ad4d05Copy full SHA for 1ad4d05
project_euler/problem_012/sol1.py
@@ -21,17 +21,20 @@
21
What is the value of the first triangle number to have over five hundred
22
divisors?
23
"""
24
-from math import sqrt
25
26
27
def count_divisors(n):
28
- nDivisors = 0
29
- for i in range(1, int(sqrt(n)) + 1):
30
- if n % i == 0:
31
- nDivisors += 2
32
- # check if n is perfect square
33
- if n ** 0.5 == int(n ** 0.5):
34
- nDivisors -= 1
+ nDivisors = 1
+ i = 2
+ while i * i <= n:
+ multiplicity = 0
+ while n % i == 0:
+ n //= i
+ multiplicity += 1
+ nDivisors *= multiplicity + 1
35
+ i += 1
36
+ if n > 1:
37
+ nDivisors *= 2
38
return nDivisors
39
40
0 commit comments