{"id":25103223,"url":"https://github.com/onebit5/fibonaccinumbercomputation","last_synced_at":"2025-07-13T19:07:34.998Z","repository":{"id":274893152,"uuid":"924405714","full_name":"Onebit5/FibonacciNumberComputation","owner":"Onebit5","description":"Largest possible Fibonacci number in C","archived":false,"fork":false,"pushed_at":"2025-01-30T00:21:08.000Z","size":23,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-02T07:12:55.710Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Onebit5.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":"2025-01-29T23:57:54.000Z","updated_at":"2025-01-30T00:23:22.000Z","dependencies_parsed_at":"2025-01-30T01:20:43.660Z","dependency_job_id":"73bd9836-0140-4fc5-a8c5-23baece8740a","html_url":"https://github.com/Onebit5/FibonacciNumberComputation","commit_stats":null,"previous_names":["onebit5/fibonaccinumbercalculator"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Onebit5/FibonacciNumberComputation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Onebit5%2FFibonacciNumberComputation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Onebit5%2FFibonacciNumberComputation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Onebit5%2FFibonacciNumberComputation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Onebit5%2FFibonacciNumberComputation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Onebit5","download_url":"https://codeload.github.com/Onebit5/FibonacciNumberComputation/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Onebit5%2FFibonacciNumberComputation/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265191122,"owners_count":23725264,"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":[],"created_at":"2025-02-07T21:39:44.663Z","updated_at":"2025-07-13T19:07:34.966Z","avatar_url":"https://github.com/Onebit5.png","language":"C","readme":"# Fibonacci Number Computation\n\nThis repository contains three C programs (`fibFirstTry.c`, `fibSecondTry.c`, and `fibThirdTry.c`) that compute Fibonacci numbers using different techniques. Each program demonstrates a unique approach to solving the problem, with increasing complexity and efficiency. Below is an explanation of the techniques used, how they work, and why the third program (`fibThirdTry.c`) cannot compute Fibonacci numbers beyond a certain limit.\n\n---\n\n## **1. `fibFirstTry.c`: Iterative Approach**\n\n### **Technique Used**\n- **Iterative Computation**:\n  This program uses a simple iterative approach to compute Fibonacci numbers. It starts with the initial values \\(F(0) = 0\\) and \\(F(1) = 1\\) and iteratively computes the next Fibonacci number until it reaches the limit of the `unsigned long long` data type.\n\n### **How It Works**\n1. Initialize `prev = 0` (for \\(F(0)\\)) and `curr = 1` (for \\(F(1)\\)).\n2. In each iteration, compute the next Fibonacci number as `next = prev + curr`.\n3. Check if `curr` exceeds the maximum value of `unsigned long long` (`ULLONG_MAX`). If so, stop and print the largest Fibonacci number found.\n4. Update `prev` and `curr` for the next iteration.\n\n### **Limitations**\n- The program stops when the next Fibonacci number exceeds `ULLONG_MAX`.\n- It is simple but inefficient for very large Fibonacci numbers due to its \\(O(n)\\) time complexity.\n\n---\n\n## **2. `fibSecondTry.c`: Matrix Exponentiation**\n\n### **Technique Used**\n- **Matrix Exponentiation**:\n  This program uses matrix exponentiation to compute Fibonacci numbers in \\(O(\\log n)\\) time. The Fibonacci sequence can be represented using the following matrix:\n  ```math\n  \\begin{pmatrix}\n  F(n) \\\\\n  F(n-1)\n  \\end{pmatrix}\n  =\n  \\begin{pmatrix}\n  1 \u0026 1 \\\\\n  1 \u0026 0\n  \\end{pmatrix}^{n-1}\n  \\begin{pmatrix}\n  F(1) \\\\\n  F(0)\n  \\end{pmatrix}\n  ```\n  \n\n### **How It Works**\n1. Define a function `matrix_multiply` to multiply two 2x2 matrices.\n2. Define a function `matrix_power` to compute the power of a matrix using exponentiation by squaring.\n3. Use the `matrix_power` function to compute the Fibonacci matrix raised to the power of \\(n-1\\).\n4. Extract \\(F(n)\\) from the resulting matrix.\n\n### **Limitations**\n- The program is incomplete and does not handle the computation of large Fibonacci numbers correctly.\n- It does not check for overflow or estimate the largest Fibonacci number that fits in `unsigned long long`.\n\n---\n\n## **3. `fibThirdTry.c`: Matrix Exponentiation with Binet's Formula**\n\n### **Technique Used**\n- **Matrix Exponentiation**:\n  Similar to `fibSecondTry.c`, this program uses matrix exponentiation to compute Fibonacci numbers efficiently.\n- **Binet's Formula**:\n  The program uses Binet's formula to estimate the largest Fibonacci number that fits in `unsigned long long`. Binet's formula is given by:\n  ```math\n  F(n) \\approx \\frac{\\phi^n}{\\sqrt{5}}, where \\phi = \\frac{1 + \\sqrt{5}}{2} \n  ```\n\n### **How It Works**\n1. Define a function `matrix_multiply` to multiply two 2x2 matrices.\n2. Define a function `matrix_power` to compute the power of a matrix using exponentiation by squaring.\n3. Use the `matrix_power` function to compute the Fibonacci matrix raised to the power of \\(n-1\\).\n4. Define a function `estimate_max_n` to estimate the largest using Binet's formula.\n5. Compute Fibonacci numbers around the estimated \\(n\\) to find the largest one that fits in `unsigned long long`.\n\n### **Why It Can't Compute Larger Fibonacci Numbers**\n- The program is limited by the size of the `unsigned long long` data type, which can only hold values up to \\(2^{64} - 1\\).\n- Once \\(F(n)\\) exceeds this limit, the program stops and reports the largest Fibonacci number that fits.\n\n---\n\n## **Comparison of the Programs**\n\n| Program          | Technique Used               | Time Complexity | Limitations                                                                 |\n|------------------|------------------------------|-----------------|-----------------------------------------------------------------------------|\n| `fibFirstTry.c`  | Iterative Approach           | \\(O(n)\\)        | Inefficient for large \\(n\\); stops at the limit of `unsigned long long`.    |\n| `fibSecondTry.c` | Matrix Exponentiation        | \\(O(\\log n)\\)   | Incomplete implementation; does not handle overflow or large \\(n\\).         |\n| `fibThirdTry.c`  | Matrix Exponentiation + Binet's Formula | \\(O(\\log n)\\) | Limited by the size of `unsigned long long`; cannot compute beyond \\(F(93)\\). |\n\n---\n\n## **Why `fibThirdTry.c` Can't Compute Larger Fibonacci Numbers**\n\nThe `fibThirdTry.c` program is designed to compute the largest Fibonacci number that fits in the `unsigned long long` data type. The limit is imposed by the maximum value that `unsigned long long` can hold, which is 2^64 - 1. Specifically:\n- The largest Fibonacci number that fits in `unsigned long long` is \\(F(93) = 1293530146158671551\\).\n- The next Fibonacci number, \\(F(94) = 18446744073709551615\\), exceeds 2^64 - 1, causing an overflow.\n\nThus, the program stops at \\(F(93)\\) and cannot compute larger Fibonacci numbers without using a different data type or arbitrary-precision arithmetic.\n\n---\n\n## **How to Run the Programs**\n\n1. Compile each program using a C compiler (e.g., `gcc`):\n   ```bash\n   gcc fibFirstTry.c -o fibFirstTry\n   gcc fibSecondTry.c -o fibSecondTry\n   gcc fibThirdTry.c -o fibThirdTry\n   ```\n2. Run the compiled programs:\n   ```bash\n   ./fibFirstTry\n   ./fibSecondTry\n   ./fibThirdTry\n   ```\n\n## **Conclusion**\n\n- `fibFirstTry.c` is a simple iterative approach but inefficient for large Fibonacci numbers.\n- `fibSecondTry.c` introduces matrix exponentiation for efficient computation but is incomplete.\n- `fibThirdTry.c` combines matrix exponentiation with Binet's formula to compute the largest Fibonacci number that fits in `unsigned long long`.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonebit5%2Ffibonaccinumbercomputation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fonebit5%2Ffibonaccinumbercomputation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fonebit5%2Ffibonaccinumbercomputation/lists"}