fib-rs
A highly optimized Fibonacci number calculator for Rust that efficiently computes arbitrarily large Fibonacci numbers.
Features
- Fast doubling algorithm: Calculates Fibonacci numbers in O(log n) time
- Handles massive inputs: Compute Fibonacci numbers up to F(10,000,000) and beyond
- Automatic optimization: Uses primitive integers for smaller inputs and BigUint for larger ones
- CLI application: Simple command-line interface for quick calculations
- Web API: HTTP server with JSON interface for network-based calculations
Installation
Usage
As a library
use fib;
// Calculate F(1000)
let input = 1000;
let result = fib;
println!;
Command-line application
# Install only the CLI (this is the default)
# Or explicitly specify the CLI feature
# Calculate the 100th Fibonacci number
Web server
# Install only the web server
# Run the server (defaults to port 8080)
# Or specify a custom port with an environment variable
PORT=3000
Then send a POST request:
Response:
Installation Options
The package provides these installation options:
--features="cli"
: Install only the command-line interface (default)--features="web"
: Install only the web server--features="cli,web"
: Install both CLI and web server--no-default-features
: Disable default features (use with other feature flags)
Performance
The implementation uses a fast doubling algorithm with the following optimizations:
- For n ≤ 185: Uses primitive u128 integers
- For n > 185: Uses BigUint for arbitrary precision
Benchmark results:
Specifications:
- 15-inch MacBook Air (2025)
- M4 Chip, 10C CPU, 10C GPU
- 32GB Unified Memory
- macOS Sequoia 15.4
Input Size | Computation Time |
---|---|
F(1,000) | ~876ns |
F(10,000) | ~8μs |
F(100,000) | ~10ms |
F(1,000,000) | ~329ms |
Algorithm Details
This implementation uses the fast doubling algorithm for Fibonacci numbers, which has 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 approach is vastly more efficient than naive recursive or iterative methods for large inputs.
License
This project is licensed under the MIT License.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.