欧拉项目python代码(12--)
第12题:
拥有超过500个因数的第一个三角数(1+2+3+4+......)
def findivisionnum(num):
count = 0
n=1
import math
while count<num:
count = 0
for i in range(1,int(math.sqrt(triangle(n)))+1):
if not triangle(n)%i:
count +=2
if int(math.sqrt(triangle(n)))==math.sqrt(triangle(n)):
count -=1
n += 1
return triangle(n-1)
def triangle(n):
return n*(n+1)/2
print findivisionnum(500)
[Finished in 13.9s]
第14题:
def findlength(n): count = 0 while n!=1: if not n%2: n /=2 count +=1 else: n =3*n+1 count +1 countcopy=count count=0 return countcopy def lengthmax(num): i=1 max_count=1 max_number=0 while i<=num: if max_count<findlength(i): max_count=findlength(i) max_number=i i +=1 else: i +=1 return max_number print lengthmax(1000000)
第15题:
def findsum1(num1): root=1 for i in range(1,2*num+1): root *= i for i in range(1,num+1): root /= i root /= i return root print findrout(20)