コード例 #1
0
ファイル: problems_21_40.py プロジェクト: dzhou/project-euler
def problem25():
    """ 
    The Fibonacci sequence is defined by the recurrence relation:
        F_(n) = F_(n-1) + F_(n-2), where F_(1) = 1 and F_(2) = 1.
    What is the first term in the Fibonacci sequence to contain 1000 digits?    
    F1 = 1
    F2 = 1
    F3 = 2
    """
    generator = mlib.fib()
    count = 1
    while True:
        n = generator.next()
        count += 1
        if len(str(n)) == 1000:
            return count
コード例 #2
0
ファイル: problems_1_20.py プロジェクト: dzhou/project-euler
def problem2():
    """    
    Each new term in the Fibonacci sequence is generated by adding 
    the previous two terms. By starting with 1 and 2, the first 10 terms will be:
    1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
    Find the sum of all the even-valued terms in the sequence which 
    do not exceed four million.
    """
    sum = 0
    generator = mlib.fib()
    while True:
        f = generator.next()
        if f > 4000000:
            break
        if f%2 == 0:
            sum += f
    return sum