【笔记】欧拉计划第二题

Q: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, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

A:(python)

n = int(input('input the number of times:\n'))
def even_value(n):
    if n < 1:
        return 'input error'
    sum = 3
    x1 = 1
    x2 = 2
    if n == 1:
        return 1
    elif n == 2:
        return 3
    for i in range(3,n+1):
        if i % 2 == 1:
            x1 = x1 + x2
            sum = sum + x1
        else:
            x2 = x1 + x2
            sum = sum + x2
    return sum / n
print(even_value(n))

猜你喜欢

转载自blog.csdn.net/xxxxfengheheda/article/details/78152773