歐拉項目python代碼(12--)

第12題:

擁有超過500個因數的第一個三角數(1+2+3+4+......)spa

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)
相關文章
相關標籤/搜索