<commit_before>#!/usr/bin/env python
#-*-coding:utf-8-*-

'''
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.
'''

import timeit

def loop(n):
    t = 0
    f = 1
    l = 1
    while l < n:
        new = f + l
        if new % 2 == 0:
            t += new
        f = l
        l = new
    return t

if __name__ == '__main__':
    print loop(4000000)
    print timeit.Timer('problem_002.loop(4000000)', 'import problem_002').timeit(100)<commit_msg>add a method using matrix<commit_after>#!/usr/bin/env python
#-*-coding:utf-8-*-

'''
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.
'''

import timeit
import numpy as np

# dynamic programming algorithm
def loop(n):
    t = 0
    f = 1
    l = 1
    while l < n:
        if f % 2 == 0:
            t += f
        new = f + l
        l = f
        f = new
    return t

# omission using matrix
def loop_matrix(n):
    t = 0
    m = np.array([[3, 2], [2, 1]])
    f = np.array([2, 1])
    while f[0] < n:
        if f[0] % 2 == 0:
            t += f[0]
        new = np.dot(m, f)
        f = new
    return t

if __name__ == '__main__':
    print loop(4000000)
    print loop_matrix(4000000)
    print timeit.Timer('problem_002.loop(4000000)', 'import problem_002').timeit(100)
    print timeit.Timer('problem_002.loop_matrix(4000000)', 'import problem_002').timeit(100)
{'fraction_non_alphanumeric': 0.12121212121212122, 'fraction_numerical': 0.07088744588744589, 'mean_word_length': 2.380255941499086, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5236506', 'n_tokens_mistral': 745, 'n_tokens_neox': 630, 'n_words': 245}