{"id":16318139,"url":"https://github.com/scorphus/f3","last_synced_at":"2025-05-13T17:16:10.559Z","repository":{"id":148200617,"uuid":"315062301","full_name":"scorphus/f3","owner":"scorphus","description":"🐍 Python playground for a blog post","archived":false,"fork":false,"pushed_at":"2020-11-23T13:04:05.000Z","size":16,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-16T20:28:09.190Z","etag":null,"topics":["blogging","fibonacci","golden-ration","lucas-numbers","project-euler","replit"],"latest_commit_sha":null,"homepage":"https://repl.it/@scorphus/f3#src/main.py","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scorphus.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-22T14:56:29.000Z","updated_at":"2020-11-23T18:55:59.000Z","dependencies_parsed_at":"2023-05-19T10:30:26.105Z","dependency_job_id":null,"html_url":"https://github.com/scorphus/f3","commit_stats":{"total_commits":21,"total_committers":1,"mean_commits":21.0,"dds":0.0,"last_synced_commit":"037d5d28773c84885acaa5f3e43f5535f1deabe2"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scorphus%2Ff3","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scorphus%2Ff3/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scorphus%2Ff3/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scorphus%2Ff3/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scorphus","download_url":"https://codeload.github.com/scorphus/f3/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253990503,"owners_count":21995776,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blogging","fibonacci","golden-ration","lucas-numbers","project-euler","replit"],"created_at":"2024-10-10T22:10:03.919Z","updated_at":"2025-05-13T17:16:10.529Z","avatar_url":"https://github.com/scorphus.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# f3 - Fibonacci For Fun\n\n## Even Fibonacci Sum\n\n[Project Euler #2](https://projecteuler.net/problem=2):\n\n\u003e Each new term in the Fibonacci sequence is generated by adding the previous\n\u003e two terms. By starting with 1 and 2, the first 10 terms will be:\n\u003e\n\u003e     1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...\n\u003e\n\u003e By considering the terms in the Fibonacci sequence whose values do not exceed\n\u003e four million, find the sum of the even-valued terms.\n\n## Solutions\n\n### Recursive\n\n```python\n# fib_01.py\nfrom functools import cache\n\n\n@cache\ndef fib(n):\n    return n if n \u003c 2 else fib(n - 2) + fib(n - 1)\n\n\ndef even_fib_sum(n):\n    i = s = 0\n    while True:\n        f = fib(i)\n        if f \u003e n:\n            return s\n        if f % 2 == 0:\n            s += f\n        i += 1\n\n\n# Time spent: 3.153654ms\n```\n\n### Recursive with iterator tools\n\n```python\n# fib_02.py\nfrom functools import cache\nfrom itertools import accumulate, cycle, takewhile\n\n\n@cache\ndef fib(n):\n    return n if n \u003c 2 else fib(n - 2) + fib(n - 1)\n\n\ndef even_fib_sum(n):\n    return sum(\n        takewhile(\n            lambda f: f \u003c= n,\n            filter(\n                lambda f: f % 2 == 0,\n                map(fib, accumulate(cycle([1]))),\n            ),\n        )\n    )\n\n\n# Time spent: 2.995692ms\n```\n\n### Iterative\n\n```python\n# fib_03.py\ndef even_fib_sum(n):\n    s, a1, a2 = 0, 1, 2\n    while a2 \u003c= n:\n        if a2 % 2 == 0:\n            s += a2\n        a2, a1 = a1 + a2, a2\n    return s\n\n\n# Time spent: 1.154399ms\n```\n\n### Iterative 3 by 3\n\n```python\n# fib_04.py\ndef even_fib_sum(n):\n    s = 0\n    a1, a2, a3 = 1, 1, 2\n    while a3 \u003c= n:\n        s += a3\n        a1 = a2 + a3\n        a2 = a1 + a3\n        a3 = a1 + a2\n    return s\n\n\n# Time spent: 0.306553ms\n```\n\n### Iterative 3 by 3, result at the end\n\n```python\n# fib_05.py\ndef even_fib_sum(n):\n    a1, a2, a3 = 1, 1, 2\n    while a3 \u003c n:\n        a1 = a2 + a3\n        a2 = a1 + a3\n        a3 = a1 + a2\n    return (a2 - 1) // 2\n\n\n# Time spent: 0.234390ms\n```\n\n### Iterative using the golden ratio\n\n```python\n# fib_06.py\nfrom math import log, sqrt\n\n\ndef even_fib_sum(n):\n    if n \u003c 1:\n        return 0\n    phi = (1 + sqrt(5)) / 2\n    N = (log(n) + log(5) / 2) // log(phi) + 1\n    num = (pow(phi, N) - pow(1 - phi, N)) // sqrt(5)\n    if num \u003e n:\n        N -= 1\n    N += 2 - (N % 3)\n    return ((pow(phi, N) - pow(1 - phi, N)) // sqrt(5) - 1) / 2\n\n\n# Time spent: 0.011343ms\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscorphus%2Ff3","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscorphus%2Ff3","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscorphus%2Ff3/lists"}