| Crates.io | fib-rs |
| lib.rs | fib-rs |
| version | 0.4.6 |
| created_at | 2025-04-01 01:08:32.962268+00 |
| updated_at | 2025-05-24 02:49:07.515261+00 |
| description | A fast Fibonacci number calculator |
| homepage | |
| repository | https://github.com/excoffierleonard/fib-rs |
| max_upload_size | |
| id | 1614279 |
| size | 120,078 |
A highly optimized Fibonacci number calculator for Rust that efficiently computes arbitrarily large Fibonacci numbers.
To use as a dependency in your project:
cargo add fib-rs --no-default-features
To install the command-line tool:
cargo install fib-rs
use fib_rs::Fib;
// Calculate F(100)
let n = 100;
let result = Fib::single(n);
// Print the result
println!("F({}) = {}", n, result);
// Calculate a range of Fibonacci numbers (F(3) through F(10))
let start = 3;
let end = 10;
let results = Fib::range(start, end);
// Print the results
(start..=end)
.zip(results.iter())
.for_each(|(i, result)| println!("F({}) = {}", i, result));
fib single 100
F(100) = 354224848179261915075
fib range 6 10
F(6) = 8
F(7) = 13
F(8) = 21
F(9) = 34
F(10) = 55
Specifications:
| Single | Computation Time |
|---|---|
| F(1,000) | ~876ns |
| F(10,000) | ~8μs |
| F(100,000) | ~332μs |
| F(1,000,000) | ~10ms |
| F(10,000,000) | ~326ms |
| Range | Computation Time |
|---|---|
| F(0) -> F(1,000) | ~57.6μs |
| F(0) -> F(10,000) | ~729μs |
| F(0) -> F(100,000) | ~45ms |
For computing a single Fibonacci number, this implementation uses the fast doubling algorithm with logarithmic time complexity:
For even n: F(2k) = F(k) (2F(k+1) - F(k))
For odd n: F(2k+1) = F(k+1)^2 + F(k)^2
This divide-and-conquer approach is vastly more efficient than naive recursive or iterative methods for large inputs.
The range implementation combines two approaches for optimal performance:
This hybrid approach provides excellent performance for generating sequences of consecutive Fibonacci numbers, especially for large ranges, by leveraging multi-core processing while maintaining mathematical efficiency.
Try the interactive web demo to calculate Fibonacci numbers directly in your browser. The demo showcases the library's performance and capabilities through a WebAssembly implementation.
This project is licensed under the MIT License.
Contributions are welcome! Please feel free to submit a Pull Request.