File tree 1 file changed +13
-11
lines changed
project_euler/problem_012
1 file changed +13
-11
lines changed Original file line number Diff line number Diff line change 21
21
What is the value of the first triangle number to have over five hundred
22
22
divisors?
23
23
"""
24
- from math import sqrt
25
24
26
25
27
26
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
27
+ nDivisors = 1
28
+ i = 2
29
+ while i * i <= n :
30
+ multiplicity = 0
31
+ while n % i == 0 :
32
+ n //= i
33
+ multiplicity += 1
34
+ nDivisors *= multiplicity + 1
35
+ i += 1
36
+ if n > 1 :
37
+ nDivisors *= 2
35
38
return nDivisors
36
39
37
40
38
41
def solution ():
39
42
"""Returns the value of the first triangle number to have over five hundred
40
43
divisors.
41
44
42
- # The code below has been commented due to slow execution affecting Travis.
43
- # >>> solution()
44
- # 76576500
45
+ >>> solution()
46
+ 76576500
45
47
"""
46
48
tNum = 1
47
49
i = 1
You can’t perform that action at this time.
0 commit comments