numberlab 0.1.9

A collection of numerical algorithms
Documentation
/// Calculates the nth term of a geometric sequence.
///
/// # Arguments
///
/// * `a` - The first term of the sequence.
/// * `r` - The common ratio of the sequence.
/// * `n` - The term number to calculate (must be greater than 0).
///
/// # Returns
///
/// The nth term of the geometric sequence.
///
/// # Panics
///
/// Panics if `n` is 0.
///
/// # Examples
///
/// ```
/// use numberlab::sequence::geometric::nth_geometric;
/// let term = nth_geometric(1.12, 2.23, 3);
/// assert_eq!(term, 1.12 * 2.23 * 2.23);
/// ```
pub fn nth_geometric(a: f64, r: f64, n: u64) -> f64 {
    match n {
        0 => panic!("'n' ({}) must be greater than 0", n),
        1 => a,
        _ => a * r.powf(n as f64 - 1.0),
    }
}

/// Generates a geometric sequence.
///
/// # Arguments
///
/// * `a` - The first term of the sequence.
/// * `r` - The common ratio of the sequence.
/// * `n` - The number of terms to generate.
///
/// # Returns
///
/// A vector containing the geometric sequence.
///
/// # Examples
///
/// ```
/// use numberlab::sequence::geometric::geometric_sequence;
/// let sequence = geometric_sequence(1.12, 2.23, 3);
/// assert_eq!(sequence, vec![1.12, 1.12 * 2.23, 1.12 * 2.23 * 2.23]);
/// ```
pub fn geometric_sequence(a: f64, r: f64, n: u64) -> Vec<f64> {
    match n {
        0 => vec![],
        1 => vec![a],
        _ => (1..=n).map(|i| nth_geometric(a, r, i)).collect(),
    }
}

/// Calculates the sum of the first `n` terms of a geometric series.
///
/// # Arguments
///
/// * `a` - The first term of the series.
/// * `r` - The common ratio of the series.
/// * `n` - The number of terms to sum.
///
/// # Returns
///
/// The sum of the first `n` terms of the geometric series.
///
/// # Examples
///
/// ```
/// use numberlab::sequence::geometric::geometric_series;
/// let sum = geometric_series(1.12, 2.23, 3);
/// assert_eq!(sum, 9.187248000000002);
/// ```
pub fn geometric_series(a: f64, r: f64, n: u64) -> f64 {
    match (n, r) {
        (0, _) => 0.0,
        (1, _) => a,
        (_, 1.0) => a * n as f64,
        _ => a * ((r.powf(n as f64) - 1f64) / (r - 1f64)),
    }
}