File tree Expand file tree Collapse file tree 1 file changed +13
-11
lines changed
project_euler/problem_012 Expand file tree Collapse file tree 1 file changed +13
-11
lines changed Original file line number Diff line number Diff line change 2121What is the value of the first triangle number to have over five hundred
2222divisors?
2323"""
24- from math import sqrt
2524
2625
2726def 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
3538 return nDivisors
3639
3740
3841def solution ():
3942 """Returns the value of the first triangle number to have over five hundred
4043 divisors.
4144
42- # The code below has been commented due to slow execution affecting Travis.
43- # >>> solution()
44- # 76576500
45+ >>> solution()
46+ 76576500
4547 """
4648 tNum = 1
4749 i = 1
You can’t perform that action at this time.
0 commit comments