Struct Fib

Source
pub struct Fib;
Expand description

A utility struct for computing Fibonacci numbers efficiently.

This struct provides static methods for calculating Fibonacci numbers using highly optimized algorithms. It supports both single Fibonacci number calculation and generating ranges of consecutive Fibonacci numbers.

The implementation uses a fast doubling algorithm for O(log n) time complexity and leverages parallel processing for range calculations to maximize performance.

For Fibonacci numbers beyond F(185), the implementation automatically switches to using BigUint for arbitrary precision, ensuring correct results for extremely large Fibonacci numbers.

Implementations§

Source§

impl Fib

Source

pub fn single(n: u128) -> BigUint

Calculate the nth Fibonacci number using an optimized fast doubling algorithm.

This method efficiently computes Fibonacci numbers of arbitrary size by using a divide-and-conquer approach based on matrix identities.

§Arguments
  • n - The index of the Fibonacci number to calculate (0-indexed, where F(0)=0, F(1)=1)
§Returns
  • The nth Fibonacci number as a BigUint
§Complexity
  • Time complexity: O(log n) due to the fast doubling algorithm
  • Space complexity: O(log n) for the recursive call stack
§Examples
use fib_rs::Fib;
use num_bigint::BigUint;
use num_traits::Zero;

assert_eq!(Fib::single(0), BigUint::zero()); // F(0) = 0
assert_eq!(Fib::single(10), BigUint::from(55u32)); // F(10) = 55
assert!(Fib::single(200) > BigUint::from(u128::MAX)); // Large value example (would overflow primitive types)
Source

pub fn range(start: u128, end: u128) -> Vec<BigUint>

Generates Fibonacci numbers for indices in the given inclusive range.

This method efficiently computes a sequence of consecutive Fibonacci numbers using parallel processing for improved performance. It divides the requested range into optimal chunks based on the available CPU threads, calculates each chunk in parallel, and then combines the results.

The implementation uses a hybrid approach that:

  1. Uses the fast doubling algorithm to efficiently find the starting values for each chunk
  2. Computes subsequent Fibonacci numbers iteratively within each chunk
  3. Processes chunks in parallel using the Rayon library
§Arguments
  • start - The starting index of the range
  • end - The ending index of the range (inclusive)
§Returns
  • A Vec<BigUint> containing ordered Fibonacci numbers for indices in the specified inclusive range.
§Complexity
  • Time complexity: Approximately O(n/t + log(start)) where n is the range size and t is the number of threads.
  • Space complexity: O(n) for storing the Fibonacci numbers in a vector.
§Performance

Performance scales with the number of available CPU cores. For large ranges, the parallelization provides significant speedup compared to sequential calculation.

§Examples
use fib_rs::Fib;
use num_bigint::BigUint;
use num_traits::{Zero, One};

// Generate Fibonacci numbers from index 3 to 10 (both 3 and 10 inclusive)
let fibs = Fib::range(3, 10);

assert_eq!(fibs.len(), 8); // indices 3 to 10
assert_eq!(fibs[0], BigUint::from(2u32)); // F(3) = 2
assert_eq!(fibs[7], BigUint::from(55u32)); // F(10) = 55

For large ranges, the parallel implementation provides significant performance improvements:

use fib_rs::Fib;

// Generate 10,000 consecutive Fibonacci numbers efficiently
let large_range = Fib::range(1000, 10999);
assert_eq!(large_range.len(), 10000);

Auto Trait Implementations§

§

impl Freeze for Fib

§

impl RefUnwindSafe for Fib

§

impl Send for Fib

§

impl Sync for Fib

§

impl Unpin for Fib

§

impl UnwindSafe for Fib

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.