Skip to content

Instantly share code, notes, and snippets.

@iizukak
Created July 20, 2012 12:04
Show Gist options
  • Select an option

  • Save iizukak/3150371 to your computer and use it in GitHub Desktop.

Select an option

Save iizukak/3150371 to your computer and use it in GitHub Desktop.
project euler problem 12
#project euler problem 12
#auther @iizukak
#素因数分解する関数(試し割り法)
def fact(n):
ans = []
sq = n ** 0.5
i = 2
while n != 1:
if i > sq:
ans.append(n)
break
if n % i == 0:
ans.append(i)
n = n / i
else:
i += 1
return ans
#素因数分解の結果をもとに約数の個数を計算する関数
def divcount(L):
tmp = [1]
for i in range(len(L) - 1):
if L[i] == L[i + 1]:
tmp[-1] += 1
else:
tmp.append(1)
ans = 1
for i in range(len(tmp)):
ans = ans * (tmp[i] + 1)
return ans
def pe12():
for i in range(1, 2 ** 20):
#using triangle number formula
tri = int((i * (i + 1)) / 2)
if (divcount(fact(tri))) > 501:
return tri
print(pe12())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment