├── .github └── workflows │ ├── cd.yml │ └── ci.yml ├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md └── src └── lib.rs /.github/workflows/cd.yml: -------------------------------------------------------------------------------- 1 | name: CD 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | jobs: 9 | build: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/checkout@v3 13 | - name: Set up Rust 14 | uses: actions-rs/toolchain@v1 15 | with: 16 | toolchain: stable 17 | override: true 18 | - name: Build project 19 | run: cargo build --release 20 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | test: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v3 16 | - name: Set up Rust 17 | uses: actions-rs/toolchain@v1 18 | with: 19 | toolchain: stable 20 | override: true 21 | - name: Run tests 22 | run: cargo test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 7 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 8 | Cargo.lock 9 | 10 | # These are backup files generated by rustfmt 11 | **/*.rs.bk 12 | 13 | # MSVC Windows builds of rustc generate these, which store debugging information 14 | *.pdb 15 | 16 | # RustRover 17 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 18 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 19 | # and can be added to the global gitignore or merged into this file. For a more nuclear 20 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 21 | #.idea/ 22 | 23 | # Added by cargo 24 | 25 | /target 26 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "fibonacci_subsequence" 3 | version = "0.1.0" 4 | edition = "2021" 5 | 6 | [dependencies] 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 aliezza hn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fibonacci Subsequence Library 2 | 3 | ![CI](https://github.com/aliezzahn/fibonacci_subsequence/actions/workflows/ci.yml/badge.svg) 4 | ![CD](https://github.com/aliezzahn/fibonacci_subsequence/actions/workflows/cd.yml/badge.svg) 5 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 6 | A Rust library to find the length of the longest Fibonacci-like subsequence in a given array of integers. 7 | 8 | --- 9 | 10 | ## Table of Contents 11 | 12 | 1. [Introduction](#introduction) 13 | 2. [Installation](#installation) 14 | 3. [Usage](#usage) 15 | 4. [Examples](#examples) 16 | 5. [Contributing](#contributing) 17 | 6. [License](#license) 18 | 7. [Contact](#contact) 19 | 20 | --- 21 | 22 | ## Introduction 23 | 24 | This library provides a function to calculate the length of the longest Fibonacci-like subsequence in a given array of integers. A Fibonacci-like subsequence is a sequence where each element is the sum of the two preceding elements, similar to the Fibonacci sequence. 25 | 26 | ### Key Features 27 | 28 | - Efficient implementation using dynamic programming. 29 | - Handles edge cases such as small arrays or arrays with no valid Fibonacci-like subsequences. 30 | - Well-documented and tested. 31 | 32 | --- 33 | 34 | ## Installation 35 | 36 | Add this library to your `Cargo.toml` file: 37 | 38 | ```toml 39 | [dependencies] 40 | fibonacci_subsequence = "0.1.0" 41 | ``` 42 | 43 | Alternatively, you can clone this repository and include it locally: 44 | 45 | ```bash 46 | git clone https://github.com/aliezzahn/fibonacci_subsequence.git 47 | cd fibonacci_subsequence 48 | ``` 49 | 50 | --- 51 | 52 | ## Usage 53 | 54 | To use the library, import the `len_longest_fib_subseq` function and pass a vector of integers to it. The function returns the length of the longest Fibonacci-like subsequence. 55 | 56 | ### Example 57 | 58 | ```rust 59 | use fibonacci_subsequence::len_longest_fib_subseq; 60 | 61 | fn main() { 62 | let arr = vec![1, 2, 3, 4, 5, 6, 7, 8]; 63 | let result = len_longest_fib_subseq(arr); 64 | println!("Length of the longest Fibonacci-like subsequence: {}", result); 65 | } 66 | ``` 67 | 68 | --- 69 | 70 | ## Examples 71 | 72 | ### Example 1 73 | 74 | ```rust 75 | let arr = vec![1, 2, 3, 4, 5, 6, 7, 8]; 76 | assert_eq!(len_longest_fib_subseq(arr), 5); 77 | ``` 78 | 79 | ### Example 2 80 | 81 | ```rust 82 | let arr = vec![1, 3, 7, 11, 12, 14, 18]; 83 | assert_eq!(len_longest_fib_subseq(arr), 3); 84 | ``` 85 | 86 | ### Example 3 (No Valid Subsequence) 87 | 88 | ```rust 89 | let arr = vec![1, 2, 4, 7, 10]; 90 | assert_eq!(len_longest_fib_subseq(arr), 0); 91 | ``` 92 | 93 | --- 94 | 95 | ## Contributing 96 | 97 | Contributions are welcome! If you'd like to contribute, please follow these steps: 98 | 99 | 1. Fork the repository. 100 | 2. Create a new branch for your feature or bugfix. 101 | 3. Make your changes and ensure all tests pass. 102 | 4. Submit a pull request with a detailed description of your changes. 103 | 104 | ### Running Tests 105 | 106 | To run the tests, use the following command: 107 | 108 | ```bash 109 | cargo test 110 | ``` 111 | 112 | ### Building Documentation 113 | 114 | To build and view the documentation, run: 115 | 116 | ```bash 117 | cargo doc --open 118 | ``` 119 | 120 | --- 121 | 122 | ## License 123 | 124 | This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. 125 | 126 | --- 127 | 128 | ## Contact 129 | 130 | For questions, suggestions, or feedback, feel free to reach out: 131 | 132 | - **Email**: [aliezzahn@gmail.com](mailto:aliezzahn@gmail.com) 133 | - **GitHub**: [aliezzahn](https://github.com/aliezzahn) 134 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | use std::collections::{HashMap, HashSet}; 2 | 3 | /// Finds the length of the longest Fibonacci-like subsequence in a given array. 4 | /// 5 | /// # Arguments 6 | /// * `arr` - A vector of integers. 7 | /// 8 | /// # Returns 9 | /// The length of the longest Fibonacci-like subsequence. If no such subsequence exists, returns 0. 10 | /// 11 | /// # Examples 12 | /// ``` 13 | /// use fibonacci_subsequence::len_longest_fib_subseq; 14 | /// 15 | /// let arr = vec![1, 2, 3, 4, 5, 6, 7, 8]; 16 | /// assert_eq!(len_longest_fib_subseq(arr), 5); 17 | /// ``` 18 | pub fn len_longest_fib_subseq(arr: Vec) -> i32 { 19 | let mut dp = HashMap::new(); 20 | let set: HashSet = arr.iter().cloned().collect(); 21 | let mut max_len = 0; 22 | 23 | for i in 0..arr.len() { 24 | for j in 0..i { 25 | let x = arr[j]; 26 | let y = arr[i]; 27 | let z = y - x; 28 | 29 | if z < x && set.contains(&z) { 30 | let len = *dp.get(&(z, x)).unwrap_or(&2) + 1; 31 | dp.insert((x, y), len); 32 | max_len = max_len.max(len); 33 | } 34 | } 35 | } 36 | 37 | max_len 38 | } 39 | 40 | #[cfg(test)] 41 | mod tests { 42 | use super::*; 43 | 44 | #[test] 45 | fn test_example_1() { 46 | let arr = vec![1, 2, 3, 4, 5, 6, 7, 8]; 47 | assert_eq!(len_longest_fib_subseq(arr), 5); 48 | } 49 | 50 | #[test] 51 | fn test_example_2() { 52 | let arr = vec![1, 3, 7, 11, 12, 14, 18]; 53 | assert_eq!(len_longest_fib_subseq(arr), 3); 54 | } 55 | 56 | #[test] 57 | fn test_no_fib_subsequence() { 58 | let arr = vec![1, 2, 4, 7, 10]; 59 | assert_eq!(len_longest_fib_subseq(arr), 0); 60 | } 61 | 62 | #[test] 63 | fn test_small_array() { 64 | let arr = vec![1, 2]; 65 | assert_eq!(len_longest_fib_subseq(arr), 0); 66 | } 67 | 68 | #[test] 69 | fn test_large_array() { 70 | let arr = vec![1, 2, 3, 5, 8, 13, 21, 34, 55, 89]; 71 | assert_eq!(len_longest_fib_subseq(arr), 10); 72 | } 73 | } --------------------------------------------------------------------------------