<jupyter_start><jupyter_text>Problem 2Each 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.<jupyter_code>def fib():
    a, b = 1, 2
    yield a  # 1
    while True:
        yield b
        a, b = b, a + b
fib()
f = fib()
next(f)
for i in range(10):
    print(next(f))
from itertools import takewhile
from operator import gt
from functools import partial
def solve(n=4000000):
    fib_below_n = takewhile(partial(gt, n), fib())
    pair_fib = (f for f in fib_below_n if f % 2 == 0)
    return sum(pair_fib)
solve()<jupyter_output><empty_output><jupyter_text>Conceitos* Geradores  OtimizaçãoGerar diretamente os pares da sequência de fibonacci<jupyter_code>def fib_pair_gen():
    a, b = 0, 2
    yield a
    while True:
        yield b
        a, b = b, 4 * b + a<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09455587392550144, 'fraction_numerical': 0.03533906399235912, 'mean_word_length': 3.4595744680851066, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '400995', 'license': 'MIT', 'n_tokens_mistral': 408, 'n_tokens_neox': 364, 'n_words': 142, 'path': 'codigo/Live071/P002.ipynb', 'repo_name': 'Engcompaulo/live-de-python', 'size': '1047'}