repo stringlengths 6 65 | file_url stringlengths 81 311 | file_path stringlengths 6 227 | content stringlengths 0 32.8k | language stringclasses 1
value | license stringclasses 7
values | commit_sha stringlengths 40 40 | retrieved_at stringdate 2026-01-04 15:31:58 2026-01-04 20:25:31 | truncated bool 2
classes |
|---|---|---|---|---|---|---|---|---|
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/collatz_sequence.rs | src/math/collatz_sequence.rs | // collatz conjecture : https://en.wikipedia.org/wiki/Collatz_conjecture
pub fn sequence(mut n: usize) -> Option<Vec<usize>> {
if n == 0 {
return None;
}
let mut list: Vec<usize> = vec![];
while n != 1 {
list.push(n);
if n.is_multiple_of(2) {
n /= 2;
} else {
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/infix_to_postfix.rs | src/math/infix_to_postfix.rs | #[derive(Debug, Clone, PartialEq, Eq)]
pub enum InfixToPostfixError {
UnknownCharacter(char),
UnmatchedParent,
}
/// Function to convert [infix expression](https://en.wikipedia.org/wiki/Infix_notation) to [postfix expression](https://en.wikipedia.org/wiki/Reverse_Polish_notation)
pub fn infix_to_postfix(infix:... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/sum_of_digits.rs | src/math/sum_of_digits.rs | /// Iteratively sums the digits of a signed integer
///
/// ## Arguments
///
/// * `num` - The number to sum the digits of
///
/// ## Examples
///
/// ```
/// use the_algorithms_rust::math::sum_digits_iterative;
///
/// assert_eq!(10, sum_digits_iterative(1234));
/// assert_eq!(12, sum_digits_iterative(-246));
/// ```
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/newton_raphson.rs | src/math/newton_raphson.rs | pub fn find_root(f: fn(f64) -> f64, fd: fn(f64) -> f64, guess: f64, iterations: i32) -> f64 {
let mut result = guess;
for _ in 0..iterations {
result = iteration(f, fd, result);
}
result
}
pub fn iteration(f: fn(f64) -> f64, fd: fn(f64) -> f64, guess: f64) -> f64 {
guess - f(guess) / fd(gue... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/pascal_triangle.rs | src/math/pascal_triangle.rs | /// ## Pascal's triangle problem
///
/// pascal_triangle(num_rows) returns the first num_rows of Pascal's triangle.\
/// About Pascal's triangle: <https://en.wikipedia.org/wiki/Pascal%27s_triangle>
///
/// # Arguments:
/// * `num_rows`: number of rows of triangle
///
/// # Complexity
/// - time complexity: O(n^2),
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/sum_of_geometric_progression.rs | src/math/sum_of_geometric_progression.rs | // Author : cyrixninja
// Find the Sum of Geometric Progression
// Wikipedia: https://en.wikipedia.org/wiki/Geometric_progression
pub fn sum_of_geometric_progression(first_term: f64, common_ratio: f64, num_of_terms: i32) -> f64 {
if common_ratio == 1.0 {
// Formula for sum if the common ratio is 1
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/armstrong_number.rs | src/math/armstrong_number.rs | pub fn is_armstrong_number(number: u32) -> bool {
let mut digits: Vec<u32> = Vec::new();
let mut num: u32 = number;
loop {
digits.push(num % 10);
num /= 10;
if num == 0 {
break;
}
}
let sum_nth_power_of_digits: u32 = digits
.iter()
.map(|... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/linear_sieve.rs | src/math/linear_sieve.rs | /*
Linear Sieve algorithm:
Time complexity is indeed O(n) with O(n) memory, but the sieve generally
runs slower than a well implemented sieve of Eratosthenes. Some use cases are:
- factorizing any number k in the sieve in O(log(k))
- calculating arbitrary multiplicative functions on sieve numbers
without increasing t... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/fast_power.rs | src/math/fast_power.rs | /// fast_power returns the result of base^power mod modulus
pub fn fast_power(mut base: usize, mut power: usize, modulus: usize) -> usize {
assert!(base >= 1);
let mut res = 1;
while power > 0 {
if power & 1 == 1 {
res = (res * base) % modulus;
}
base = (base * base) % m... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/amicable_numbers.rs | src/math/amicable_numbers.rs | // Operations based around amicable numbers
// Suports u32 but should be interchangable with other types
// Wikipedia reference: https://en.wikipedia.org/wiki/Amicable_numbers
// Returns vec of amicable pairs below N
// N must be positive
pub fn amicable_pairs_under_n(n: u32) -> Option<Vec<(u32, u32)>> {
let mut f... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/huber_loss.rs | src/math/huber_loss.rs | //! # Huber Loss Function
//!
//! The `huber_loss` function calculates the Huber loss, which is a robust loss function used in machine learning, particularly in regression problems.
//!
//! Huber loss combines the benefits of mean squared error (MSE) and mean absolute error (MAE). It behaves like MSE when the differenc... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/leaky_relu.rs | src/math/leaky_relu.rs | //! # Leaky ReLU Function
//!
//! The `leaky_relu` function computes the Leaky Rectified Linear Unit (ReLU) values of a given vector
//! of f64 numbers with a specified alpha parameter.
//!
//! The Leaky ReLU activation function is commonly used in neural networks to introduce a small negative
//! slope (controlled by ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/abs.rs | src/math/abs.rs | /// This function returns the absolute value of a number.\
/// The absolute value of a number is the non-negative value of the number, regardless of its sign.\
///
/// Wikipedia: <https://en.wikipedia.org/wiki/Absolute_value>
pub fn abs<T>(num: T) -> T
where
T: std::ops::Neg<Output = T> + PartialOrd + Copy + num_tr... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/prime_check.rs | src/math/prime_check.rs | pub fn prime_check(num: usize) -> bool {
if (num > 1) & (num < 4) {
return true;
} else if (num < 2) || (num.is_multiple_of(2)) {
return false;
}
let stop: usize = (num as f64).sqrt() as usize + 1;
for i in (3..stop).step_by(2) {
if num.is_multiple_of(i) {
return... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/karatsuba_multiplication.rs | src/math/karatsuba_multiplication.rs | /*
Finds the product of two numbers using Karatsuba Algorithm
*/
use std::cmp::max;
const TEN: i128 = 10;
pub fn multiply(num1: i128, num2: i128) -> i128 {
_multiply(num1, num2)
}
fn _multiply(num1: i128, num2: i128) -> i128 {
if num1 < 10 || num2 < 10 {
return num1 * num2;
}
let mut num1_str... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/trapezoidal_integration.rs | src/math/trapezoidal_integration.rs | pub fn trapezoidal_integral<F>(a: f64, b: f64, f: F, precision: u32) -> f64
where
F: Fn(f64) -> f64,
{
let delta = (b - a) / precision as f64;
(0..precision)
.map(|trapezoid| {
let left_side = a + (delta * trapezoid as f64);
let right_side = left_side + delta;
0... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/modular_exponential.rs | src/math/modular_exponential.rs | /// Calculate the greatest common divisor (GCD) of two numbers and the
/// coefficients of Bézout's identity using the Extended Euclidean Algorithm.
///
/// # Arguments
///
/// * `a` - One of the numbers to find the GCD of
/// * `m` - The other number to find the GCD of
///
/// # Returns
///
/// A tuple (gcd, x1, x2) s... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/perfect_cube.rs | src/math/perfect_cube.rs | // Check if a number is a perfect cube using binary search.
pub fn perfect_cube_binary_search(n: i64) -> bool {
if n < 0 {
return perfect_cube_binary_search(-n);
}
// Initialize left and right boundaries for binary search.
let mut left = 0;
let mut right = n.abs(); // Use the absolute value... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/perfect_square.rs | src/math/perfect_square.rs | // Author : cyrixninja
// Perfect Square : Checks if a number is perfect square number or not
// https://en.wikipedia.org/wiki/Perfect_square
pub fn perfect_square(num: i32) -> bool {
if num < 0 {
return false;
}
let sqrt_num = (num as f64).sqrt() as i32;
sqrt_num * sqrt_num == num
}
pub fn per... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/sieve_of_eratosthenes.rs | src/math/sieve_of_eratosthenes.rs | /// Implements the Sieve of Eratosthenes algorithm to find all prime numbers up to a given limit.
///
/// # Arguments
///
/// * `num` - The upper limit up to which to find prime numbers (inclusive).
///
/// # Returns
///
/// A vector containing all prime numbers up to the specified limit.
pub fn sieve_of_eratosthenes(n... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/gaussian_error_linear_unit.rs | src/math/gaussian_error_linear_unit.rs | //! # Gaussian Error Linear Unit (GELU) Function
//!
//! The `gaussian_error_linear_unit` function computes the Gaussian Error Linear Unit (GELU) values of a given f64 number or a vector of f64 numbers.
//!
//! GELU is an activation function used in neural networks that introduces a smooth approximation of the rectifie... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/zellers_congruence_algorithm.rs | src/math/zellers_congruence_algorithm.rs | // returns the day of the week from the Gregorian Date
pub fn zellers_congruence_algorithm(date: i32, month: i32, year: i32, as_string: bool) -> String {
let q = date;
let (m, y) = if month < 3 {
(month + 12, year - 1)
} else {
(month, year)
};
let day: i32 =
(q + (26 * (m +... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/softmax.rs | src/math/softmax.rs | //! # Softmax Function
//!
//! The `softmax` function computes the softmax values of a given array of f32 numbers.
//!
//! The softmax operation is often used in machine learning for converting a vector of real numbers into a
//! probability distribution. It exponentiates each element in the input array, and then norma... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/square_root.rs | src/math/square_root.rs | /// squre_root returns the square root
/// of a f64 number using Newton's method
pub fn square_root(num: f64) -> f64 {
if num < 0.0_f64 {
return f64::NAN;
}
let mut root = 1.0_f64;
while (root * root - num).abs() > 1e-10_f64 {
root -= (root * root - num) / (2.0_f64 * root);
}
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/postfix_evaluation.rs | src/math/postfix_evaluation.rs | //! This module provides a function to evaluate postfix (Reverse Polish Notation) expressions.
//! Postfix notation is a mathematical notation in which every operator follows all of its operands.
//!
//! The evaluator supports the four basic arithmetic operations: addition, subtraction, multiplication, and division.
//... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/binomial_coefficient.rs | src/math/binomial_coefficient.rs | extern crate num_bigint;
extern crate num_traits;
use num_bigint::BigInt;
use num_traits::FromPrimitive;
/// Calculate binomial coefficient (n choose k).
///
/// This function computes the binomial coefficient C(n, k) using BigInt
/// for arbitrary precision arithmetic.
///
/// Formula:
/// C(n, k) = n! / (k! * (n - ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/area_of_polygon.rs | src/math/area_of_polygon.rs | /**
* @file
* @brief Calculate the area of a polygon defined by a vector of points.
*
* @details
* This program provides a function to calculate the area of a polygon defined by a vector of points.
* The area is calculated using the formula: A = |Σ((xi - xi-1) * (yi + yi-1))| / 2
* where (xi, yi) are the coordin... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/factors.rs | src/math/factors.rs | /// Factors are natural numbers which can divide a given natural number to give a remainder of zero
/// Hence 1, 2, 3 and 6 are all factors of 6, as they divide the number 6 completely,
/// leaving no remainder.
/// This function is to list out all the factors of a given number 'n'
pub fn factors(number: u64) -> Vec<u... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/extended_euclidean_algorithm.rs | src/math/extended_euclidean_algorithm.rs | fn update_step(a: &mut i32, old_a: &mut i32, quotient: i32) {
let temp = *a;
*a = *old_a - quotient * temp;
*old_a = temp;
}
pub fn extended_euclidean_algorithm(a: i32, b: i32) -> (i32, i32, i32) {
let (mut old_r, mut rem) = (a, b);
let (mut old_s, mut coeff_s) = (1, 0);
let (mut old_t, mut coe... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/faster_perfect_numbers.rs | src/math/faster_perfect_numbers.rs | use super::{mersenne_primes::is_mersenne_prime, prime_numbers::prime_numbers};
use std::convert::TryInto;
/*
Generates a list of perfect numbers till `num` using the Lucas Lehmer test algorithm.
url : https://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test
*/
pub fn generate_perfect_numbers(num: usiz... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/signum.rs | src/math/signum.rs | /// Signum function is a mathematical function that extracts
/// the sign of a real number. It is also known as the sign function,
/// and it is an odd piecewise function.
/// If a number is negative, i.e. it is less than zero, then sgn(x) = -1
/// If a number is zero, then sgn(0) = 0
/// If a number is positive, i.e. ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/sum_of_harmonic_series.rs | src/math/sum_of_harmonic_series.rs | // Author : cyrixninja
// Sum of Harmonic Series : Find the sum of n terms in an harmonic progression. The calculation starts with the
// first_term and loops adding the common difference of Arithmetic Progression by which
// the given Harmonic Progression is ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/baby_step_giant_step.rs | src/math/baby_step_giant_step.rs | use crate::math::greatest_common_divisor;
/// Baby-step Giant-step algorithm
///
/// Solving discrete logarithm problem:
/// a^x = b (mod n) , with respect to gcd(a, n) == 1
/// with O(sqrt(n)) time complexity.
///
/// Wikipedia reference: https://en.wikipedia.org/wiki/Baby-step_giant-step
/// When a is the primiti... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/area_under_curve.rs | src/math/area_under_curve.rs | pub fn area_under_curve(start: f64, end: f64, func: fn(f64) -> f64, step_count: usize) -> f64 {
assert!(step_count > 0);
let (start, end) = if start > end {
(end, start)
} else {
(start, end)
}; //swap if bounds reversed
let step_length: f64 = (end - start) / step_count as f64;
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/trial_division.rs | src/math/trial_division.rs | fn floor(value: f64, scale: u8) -> f64 {
let multiplier = 10i64.pow(scale as u32) as f64;
(value * multiplier).floor()
}
fn double_to_int(amount: f64) -> i128 {
amount.round() as i128
}
pub fn trial_division(mut num: i128) -> Vec<i128> {
if num < 0 {
return trial_division(-num);
}
let ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/euclidean_distance.rs | src/math/euclidean_distance.rs | // Author : cyrixninja
// Calculate the Euclidean distance between two vectors
// Wikipedia : https://en.wikipedia.org/wiki/Euclidean_distance
pub fn euclidean_distance(vector_1: &Vector, vector_2: &Vector) -> f64 {
// Calculate the Euclidean distance using the provided vectors.
let squared_sum: f64 = vector_1... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/interest.rs | src/math/interest.rs | // value of e
use std::f64::consts::E;
// function to calculate simple interest
pub fn simple_interest(principal: f64, annual_rate: f64, years: f64) -> (f64, f64) {
let interest = principal * annual_rate * years;
let value = principal * (1.0 + (annual_rate * years));
println!("Interest earned: {interest}"... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/tanh.rs | src/math/tanh.rs | //Rust implementation of the Tanh (hyperbolic tangent) activation function.
//The formula for Tanh: (e^x - e^(-x))/(e^x + e^(-x)) OR (2/(1+e^(-2x))-1
//More information on the concepts of Sigmoid can be found here:
//https://en.wikipedia.org/wiki/Hyperbolic_functions
//The function below takes a reference to a mutable... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/matrix_ops.rs | src/math/matrix_ops.rs | // Basic matrix operations using a Matrix type with internally uses
// a vector representation to store matrix elements.
// Generic using the MatrixElement trait, which can be implemented with
// the matrix_element_type_def macro.
// Wikipedia reference: https://www.wikiwand.com/en/Matrix_(mathematics)
use std::ops::{A... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/catalan_numbers.rs | src/math/catalan_numbers.rs | // Introduction to Catalan Numbers:
// Catalan numbers are a sequence of natural numbers with many applications in combinatorial mathematics.
// They are named after the Belgian mathematician Eugène Charles Catalan, who contributed to their study.
// Catalan numbers appear in various combinatorial problems, including c... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/greatest_common_divisor.rs | src/math/greatest_common_divisor.rs | /// Greatest Common Divisor.
///
/// greatest_common_divisor(num1, num2) returns the greatest number of num1 and num2.
///
/// Wikipedia reference: https://en.wikipedia.org/wiki/Greatest_common_divisor
/// gcd(a, b) = gcd(a, -b) = gcd(-a, b) = gcd(-a, -b) by definition of divisibility
use std::cmp::{max, min};
pub fn ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/prime_numbers.rs | src/math/prime_numbers.rs | pub fn prime_numbers(max: usize) -> Vec<usize> {
let mut result: Vec<usize> = Vec::new();
if max >= 2 {
result.push(2)
}
for i in (3..=max).step_by(2) {
let stop: usize = (i as f64).sqrt() as usize + 1;
let mut status = true;
for j in (3..stop).step_by(2) {
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/mod.rs | src/math/mod.rs | mod abs;
mod aliquot_sum;
mod amicable_numbers;
mod area_of_polygon;
mod area_under_curve;
mod armstrong_number;
mod average;
mod baby_step_giant_step;
mod bell_numbers;
mod binary_exponentiation;
mod binomial_coefficient;
mod catalan_numbers;
mod ceil;
mod chinese_remainder_theorem;
mod collatz_sequence;
mod combinati... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/trig_functions.rs | src/math/trig_functions.rs | /// Function that contains the similarities of the sine and cosine implementations
///
/// Both of them are calculated using their MacLaurin Series
///
/// Because there is just a '+1' that differs in their formula, this function has been
/// created for not repeating
fn template<T: Into<f64>>(x: T, tol: f64, kind: i32... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/binary_exponentiation.rs | src/math/binary_exponentiation.rs | // Binary exponentiation is an algorithm to compute a power in O(logN) where N is the power.
//
// For example, to naively compute n^100, we multiply n 99 times for a O(N) algorithm.
//
// With binary exponentiation we can reduce the number of muliplications by only finding the binary
// exponents. n^100 = n^64 * n^32 ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/sprague_grundy_theorem.rs | src/math/sprague_grundy_theorem.rs | /**
* Sprague Grundy Theorem for combinatorial games like Nim
*
* The Sprague Grundy Theorem is a fundamental concept in combinatorial game theory, commonly used to analyze
* games like Nim. It calculates the Grundy number (also known as the nimber) for a position in a game.
* The Grundy number represents the game... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/ceil.rs | src/math/ceil.rs | // In mathematics and computer science, the ceiling function maps x to the least integer greater than or equal to x
// Source: https://en.wikipedia.org/wiki/Floor_and_ceiling_functions
pub fn ceil(x: f64) -> f64 {
let x_rounded_towards_zero = x as i32 as f64;
if x < 0. || x_rounded_towards_zero == x {
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/pollard_rho.rs | src/math/pollard_rho.rs | use super::miller_rabin;
struct LinearCongruenceGenerator {
// modulus as 2 ^ 32
multiplier: u32,
increment: u32,
state: u32,
}
impl LinearCongruenceGenerator {
fn new(multiplier: u32, increment: u32, state: u32) -> Self {
Self {
multiplier,
increment,
s... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/mersenne_primes.rs | src/math/mersenne_primes.rs | // mersenne prime : https://en.wikipedia.org/wiki/Mersenne_prime
pub fn is_mersenne_prime(n: usize) -> bool {
if n == 2 {
return true;
}
let mut s = 4;
let m = 2_usize.pow(std::convert::TryInto::try_into(n).unwrap()) - 1;
for _ in 0..n - 2 {
s = ((s * s) - 2) % m;
}
s == 0
}
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/simpsons_integration.rs | src/math/simpsons_integration.rs | pub fn simpsons_integration<F>(f: F, a: f64, b: f64, n: usize) -> f64
where
F: Fn(f64) -> f64,
{
let h = (b - a) / n as f64;
(0..n)
.map(|i| {
let x0 = a + i as f64 * h;
let x1 = x0 + h / 2.0;
let x2 = x0 + h;
(h / 6.0) * (f(x0) + 4.0 * f(x1) + f(x2))
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/fast_fourier_transform.rs | src/math/fast_fourier_transform.rs | use std::ops::{Add, Mul, MulAssign, Sub};
// f64 complex
#[derive(Clone, Copy, Debug)]
pub struct Complex64 {
pub re: f64,
pub im: f64,
}
impl Complex64 {
#[inline]
pub fn new(re: f64, im: f64) -> Self {
Self { re, im }
}
#[inline]
pub fn square_norm(&self) -> f64 {
self.r... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/sylvester_sequence.rs | src/math/sylvester_sequence.rs | // Author : cyrixninja
// Sylvester Series : Calculates the nth number in Sylvester's sequence.
// Wikipedia Reference : https://en.wikipedia.org/wiki/Sylvester%27s_sequence
// Other References : https://the-algorithms.com/algorithm/sylvester-sequence?lang=python
pub fn sylvester(number: i32) -> i128 {
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/cross_entropy_loss.rs | src/math/cross_entropy_loss.rs | //! # Cross-Entropy Loss Function
//!
//! The `cross_entropy_loss` function calculates the cross-entropy loss between the actual and predicted probability distributions.
//!
//! Cross-entropy loss is commonly used in machine learning and deep learning to measure the dissimilarity between two probability distributions. ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/vector_cross_product.rs | src/math/vector_cross_product.rs | /// Cross Product and Magnitude Calculation
///
/// This program defines functions to calculate the cross product of two 3D vectors
/// and the magnitude of a vector from its direction ratios. The main purpose is
/// to demonstrate the mathematical concepts and provide test cases for the functions.
///
/// Time Complex... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/prime_factors.rs | src/math/prime_factors.rs | // Finds the prime factors of a number in increasing order, with repetition.
pub fn prime_factors(n: u64) -> Vec<u64> {
let mut i = 2;
let mut n = n;
let mut factors = Vec::new();
while i * i <= n {
if n.is_multiple_of(i) {
n /= i;
factors.push(i);
} else {
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/interpolation.rs | src/math/interpolation.rs | /// In mathematics, linear interpolation is a method of curve fitting
/// using linear polynomials to construct new data points within the range of a discrete set of known data points.
/// Formula: y = y0 + (x - x0) * (y1 - y0) / (x1 - x0)
/// Source: https://en.wikipedia.org/wiki/Linear_interpolation
/// point0 and po... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/lucas_series.rs | src/math/lucas_series.rs | // Author : cyrixninja
// Lucas Series : Function to get the Nth Lucas Number
// Wikipedia Reference : https://en.wikipedia.org/wiki/Lucas_number
// Other References : https://the-algorithms.com/algorithm/lucas-series?lang=python
pub fn recursive_lucas_number(n: u32) -> u32 {
match n {
0 => 2,
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/aliquot_sum.rs | src/math/aliquot_sum.rs | /// Aliquot sum of a number is defined as the sum of the proper divisors of a number.\
/// i.e. all the divisors of a number apart from the number itself.
///
/// ## Example:
/// The aliquot sum of 6 is (1 + 2 + 3) = 6, and that of 15 is (1 + 3 + 5) = 9
///
/// Wikipedia article on Aliquot Sum: <https://en.wikipedia.or... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/logarithm.rs | src/math/logarithm.rs | use std::f64::consts::E;
/// Calculates the **log<sub>base</sub>(x)**
///
/// Parameters:
/// <p>-> base: base of log
/// <p>-> x: value for which log shall be evaluated
/// <p>-> tol: tolerance; the precision of the approximation (submultiples of 10<sup>-1</sup>)
///
/// Advisable to use **std::f64::consts::***... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/lcm_of_n_numbers.rs | src/math/lcm_of_n_numbers.rs | // returns the least common multiple of n numbers
pub fn lcm(nums: &[usize]) -> usize {
if nums.len() == 1 {
return nums[0];
}
let a = nums[0];
let b = lcm(&nums[1..]);
a * b / gcd_of_two_numbers(a, b)
}
fn gcd_of_two_numbers(a: usize, b: usize) -> usize {
if b == 0 {
return a;... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/least_square_approx.rs | src/math/least_square_approx.rs | /// Least Square Approximation <p>
/// Function that returns a polynomial which very closely passes through the given points (in 2D)
///
/// The result is made of coeficients, in descending order (from x^degree to free term)
///
/// Parameters:
///
/// points -> coordinates of given points
///
/// degree -> degree of t... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/miller_rabin.rs | src/math/miller_rabin.rs | use num_bigint::BigUint;
use num_traits::{One, ToPrimitive, Zero};
use std::cmp::Ordering;
fn modulo_power(mut base: u64, mut power: u64, modulo: u64) -> u64 {
base %= modulo;
if base == 0 {
return 0; // return zero if base is divisible by modulo
}
let mut ans: u128 = 1;
let mut bbase: u128... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/frizzy_number.rs | src/math/frizzy_number.rs | /// This Rust program calculates the n-th Frizzy number for a given base.
/// A Frizzy number is defined as the n-th number that is a sum of powers
/// of the given base, with the powers corresponding to the binary representation
/// of n.
/// The `get_nth_frizzy` function takes two arguments:
/// * `base` - The base ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/factorial.rs | src/math/factorial.rs | use num_bigint::BigUint;
use num_traits::One;
#[allow(unused_imports)]
use std::str::FromStr;
pub fn factorial(number: u64) -> u64 {
// Base cases: 0! and 1! are both equal to 1
if number == 0 || number == 1 {
1
} else {
// Calculate factorial using the product of the range from 2 to the gi... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/sigmoid.rs | src/math/sigmoid.rs | //Rust implementation of the Sigmoid activation function.
//The formula for Sigmoid: 1 / (1 + e^(-x))
//More information on the concepts of Sigmoid can be found here:
//https://en.wikipedia.org/wiki/Sigmoid_function
//The function below takes a reference to a mutable <f32> Vector as an argument
//and returns the vecto... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/chinese_remainder_theorem.rs | src/math/chinese_remainder_theorem.rs | use super::extended_euclidean_algorithm;
fn mod_inv(x: i32, n: i32) -> Option<i32> {
let (g, x, _) = extended_euclidean_algorithm(x, n);
if g == 1 {
Some((x % n + n) % n)
} else {
None
}
}
pub fn chinese_remainder_theorem(residues: &[i32], modulli: &[i32]) -> Option<i32> {
let prod... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/geometric_series.rs | src/math/geometric_series.rs | // Author : cyrixninja
// Wikipedia : https://en.wikipedia.org/wiki/Geometric_series
// Calculate a geometric series.
pub fn geometric_series(nth_term: f64, start_term_a: f64, common_ratio_r: f64) -> Vec<f64> {
let mut series = Vec::new();
let mut multiple = 1.0;
for _ in 0..(nth_term as i32) {
se... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/combinations.rs | src/math/combinations.rs | // Function to calculate combinations of k elements from a set of n elements
pub fn combinations(n: i64, k: i64) -> i64 {
// Check if either n or k is negative, and panic if so
if n < 0 || k < 0 {
panic!("Please insert positive values");
}
let mut res: i64 = 1;
for i in 0..k {
// Ca... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/gcd_of_n_numbers.rs | src/math/gcd_of_n_numbers.rs | /// returns the greatest common divisor of n numbers
pub fn gcd(nums: &[usize]) -> usize {
if nums.len() == 1 {
return nums[0];
}
let a = nums[0];
let b = gcd(&nums[1..]);
gcd_of_two_numbers(a, b)
}
fn gcd_of_two_numbers(a: usize, b: usize) -> usize {
if b == 0 {
return a;
}... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/math/nthprime.rs | src/math/nthprime.rs | // Generate the nth prime number.
// Algorithm is inspired by the optimized version of the Sieve of Eratosthenes.
pub fn nthprime(nth: u64) -> u64 {
let mut total_prime: u64 = 0;
let mut size_factor: u64 = 2;
let mut s: u64 = nth * size_factor;
let mut primes: Vec<u64> = Vec::new();
let n: u64 = n... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/geometry/polygon_points.rs | src/geometry/polygon_points.rs | type Ll = i64;
type Pll = (Ll, Ll);
fn cross(x1: Ll, y1: Ll, x2: Ll, y2: Ll) -> Ll {
x1 * y2 - x2 * y1
}
pub fn polygon_area(pts: &[Pll]) -> Ll {
let mut ats = 0;
for i in 2..pts.len() {
ats += cross(
pts[i].0 - pts[0].0,
pts[i].1 - pts[0].1,
pts[i - 1].0 - pts[... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/geometry/closest_points.rs | src/geometry/closest_points.rs | use crate::geometry::Point;
use std::cmp::Ordering;
fn cmp_x(p1: &Point, p2: &Point) -> Ordering {
let acmp = f64_cmp(&p1.x, &p2.x);
match acmp {
Ordering::Equal => f64_cmp(&p1.y, &p2.y),
_ => acmp,
}
}
fn f64_cmp(a: &f64, b: &f64) -> Ordering {
a.partial_cmp(b).unwrap()
}
/// returns... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/geometry/mod.rs | src/geometry/mod.rs | mod closest_points;
mod graham_scan;
mod jarvis_scan;
mod point;
mod polygon_points;
mod ramer_douglas_peucker;
mod segment;
pub use self::closest_points::closest_points;
pub use self::graham_scan::graham_scan;
pub use self::jarvis_scan::jarvis_march;
pub use self::point::Point;
pub use self::polygon_points::lattice_p... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/geometry/jarvis_scan.rs | src/geometry/jarvis_scan.rs | use crate::geometry::Point;
use crate::geometry::Segment;
// Returns a Vec of Points that make up the convex hull of `points`. Returns an empty Vec if there
// is no convex hull.
pub fn jarvis_march(points: Vec<Point>) -> Vec<Point> {
if points.len() <= 2 {
return vec![];
}
let mut convex_hull = v... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/geometry/ramer_douglas_peucker.rs | src/geometry/ramer_douglas_peucker.rs | use crate::geometry::Point;
pub fn ramer_douglas_peucker(points: &[Point], epsilon: f64) -> Vec<Point> {
if points.len() < 3 {
return points.to_vec();
}
let mut dmax = 0.0;
let mut index = 0;
let end = points.len() - 1;
for i in 1..end {
let d = perpendicular_distance(&points[i... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/geometry/segment.rs | src/geometry/segment.rs | use super::Point;
const TOLERANCE: f64 = 0.0001;
pub struct Segment {
pub a: Point,
pub b: Point,
}
impl Segment {
pub fn new(x1: f64, y1: f64, x2: f64, y2: f64) -> Segment {
Segment {
a: Point::new(x1, y1),
b: Point::new(x2, y2),
}
}
pub fn from_points(a:... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/geometry/graham_scan.rs | src/geometry/graham_scan.rs | use crate::geometry::Point;
use std::cmp::Ordering;
fn point_min(a: &&Point, b: &&Point) -> Ordering {
// Find the bottom-most point. In the case of a tie, find the left-most.
if a.y == b.y {
a.x.partial_cmp(&b.x).unwrap()
} else {
a.y.partial_cmp(&b.y).unwrap()
}
}
// Returns a Vec of... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/geometry/point.rs | src/geometry/point.rs | use std::ops::Sub;
#[derive(Clone, Debug, PartialEq)]
pub struct Point {
pub x: f64,
pub y: f64,
}
impl Point {
pub fn new(x: f64, y: f64) -> Point {
Point { x, y }
}
// Returns the orientation of consecutive segments ab and bc.
pub fn consecutive_orientation(&self, b: &Point, c: &Poi... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/jump_search.rs | src/searching/jump_search.rs | use std::cmp::min;
pub fn jump_search<T: Ord>(item: &T, arr: &[T]) -> Option<usize> {
let len = arr.len();
if len == 0 {
return None;
}
let mut step = (len as f64).sqrt() as usize;
let mut prev = 0;
while &arr[min(len, step) - 1] < item {
prev = step;
step += (len as f6... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/binary_search.rs | src/searching/binary_search.rs | //! This module provides an implementation of a binary search algorithm that
//! works for both ascending and descending ordered arrays. The binary search
//! function returns the index of the target element if it is found, or `None`
//! if the target is not present in the array.
use std::cmp::Ordering;
/// Performs ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/quick_select.rs | src/searching/quick_select.rs | // https://en.wikipedia.org/wiki/Quickselect
fn partition(list: &mut [i32], left: usize, right: usize, pivot_index: usize) -> usize {
let pivot_value = list[pivot_index];
list.swap(pivot_index, right); // Move pivot to end
let mut store_index = left;
for i in left..right {
if list[i] < pivot_va... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/saddleback_search.rs | src/searching/saddleback_search.rs | // Saddleback search is a technique used to find an element in a sorted 2D matrix in O(m + n) time,
// where m is the number of rows, and n is the number of columns. It works by starting from the
// top-right corner of the matrix and moving left or down based on the comparison of the current
// element with the target ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/binary_search_recursive.rs | src/searching/binary_search_recursive.rs | use std::cmp::Ordering;
/// Recursively performs a binary search for a specified item within a sorted array.
///
/// This function can handle both ascending and descending ordered arrays. It
/// takes a reference to the item to search for and a slice of the array. If
/// the item is found, it returns the index of the ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/ternary_search_min_max_recursive.rs | src/searching/ternary_search_min_max_recursive.rs | /// Recursive ternary search algorithm for finding maximum of unimodal function
pub fn ternary_search_max_rec(
f: fn(f32) -> f32,
start: f32,
end: f32,
absolute_precision: f32,
) -> f32 {
if (end - start).abs() >= absolute_precision {
let mid1 = start + (end - start) / 3.0;
let mid2 ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/mod.rs | src/searching/mod.rs | mod binary_search;
mod binary_search_recursive;
mod exponential_search;
mod fibonacci_search;
mod interpolation_search;
mod jump_search;
mod kth_smallest;
mod kth_smallest_heap;
mod linear_search;
mod moore_voting;
mod quick_select;
mod saddleback_search;
mod ternary_search;
mod ternary_search_min_max;
mod ternary_sear... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/linear_search.rs | src/searching/linear_search.rs | /// Performs a linear search on the given array, returning the index of the first occurrence of the item.
///
/// # Arguments
///
/// * `item` - A reference to the item to search for in the array.
/// * `arr` - A slice of items to search within.
///
/// # Returns
///
/// * `Some(usize)` - The index of the first occurre... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/ternary_search.rs | src/searching/ternary_search.rs | //! This module provides an implementation of a ternary search algorithm that
//! works for both ascending and descending ordered arrays. The ternary search
//! function returns the index of the target element if it is found, or `None`
//! if the target is not present in the array.
use std::cmp::Ordering;
/// Perform... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/ternary_search_recursive.rs | src/searching/ternary_search_recursive.rs | use std::cmp::Ordering;
pub fn ternary_search_rec<T: Ord>(
target: &T,
list: &[T],
start: usize,
end: usize,
) -> Option<usize> {
if list.is_empty() {
return None;
}
if end >= start {
let mid1: usize = start + (end - start) / 3;
let mid2: usize = end - (end - start)... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/moore_voting.rs | src/searching/moore_voting.rs | /*
Moore's voting algorithm finds out the strictly majority-occurring element
without using extra space
and O(n) + O(n) time complexity
It is built on the intuition that a strictly major element will always have a net occurrence as 1.
Say, array given: 9 1 8 1 1
Here, the algorithm will work a... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/kth_smallest_heap.rs | src/searching/kth_smallest_heap.rs | use crate::data_structures::Heap;
use std::cmp::{Ord, Ordering};
/// Returns k-th smallest element of an array.
/// Time complexity is stably O(nlog(k)) in all cases
/// Extra space is required to maintain the heap, and it doesn't
/// mutate the input list.
///
/// It is preferrable to the partition-based algorithm in... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/interpolation_search.rs | src/searching/interpolation_search.rs | pub fn interpolation_search<Ordering>(nums: &[i32], item: &i32) -> Result<usize, usize> {
// early check
if nums.is_empty() {
return Err(0);
}
let mut low: usize = 0;
let mut high: usize = nums.len() - 1;
while low <= high {
if *item < nums[low] || *item > nums[high] {
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/kth_smallest.rs | src/searching/kth_smallest.rs | use crate::sorting::partition;
use std::cmp::{Ordering, PartialOrd};
/// Returns k-th smallest element of an array, i.e. its order statistics.
/// Time complexity is O(n^2) in the worst case, but only O(n) on average.
/// It mutates the input, and therefore does not require additional space.
pub fn kth_smallest<T>(inp... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/fibonacci_search.rs | src/searching/fibonacci_search.rs | use std::cmp::min;
use std::cmp::Ordering;
pub fn fibonacci_search<T: Ord>(item: &T, arr: &[T]) -> Option<usize> {
let len = arr.len();
if len == 0 {
return None;
}
let mut start = -1;
let mut f0 = 0;
let mut f1 = 1;
let mut f2 = 1;
while f2 < len {
f0 = f1;
f1 ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/ternary_search_min_max.rs | src/searching/ternary_search_min_max.rs | /// Ternary search algorithm for finding maximum of unimodal function
pub fn ternary_search_max(
f: fn(f32) -> f32,
mut start: f32,
mut end: f32,
absolute_precision: f32,
) -> f32 {
while (start - end).abs() >= absolute_precision {
let mid1 = start + (end - start) / 3.0;
let mid2 = e... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/searching/exponential_search.rs | src/searching/exponential_search.rs | use std::cmp::Ordering;
pub fn exponential_search<T: Ord>(item: &T, arr: &[T]) -> Option<usize> {
let len = arr.len();
if len == 0 {
return None;
}
let mut upper = 1;
while (upper < len) && (&arr[upper] <= item) {
upper *= 2;
}
if upper > len {
upper = len
}
... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/conversions/order_of_magnitude_conversion.rs | src/conversions/order_of_magnitude_conversion.rs | //! Length Unit Conversion
//!
//! This module provides conversion between metric length units ranging from
//! meters to yottametres (10^24 meters).
//!
//! Available units: Meter, Kilometer, Megametre, Gigametre, Terametre,
//! Petametre, Exametre, Zettametre, Yottametre
//!
//! # References
//!
//! - [Meter - Wikipe... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/conversions/decimal_to_octal.rs | src/conversions/decimal_to_octal.rs | // Author: NithinU2802
// Decimal to Octal Converter: Converts Decimal to Octal
// Wikipedia References:
// 1. https://en.wikipedia.org/wiki/Decimal
// 2. https://en.wikipedia.org/wiki/Octal
pub fn decimal_to_octal(decimal_num: u64) -> String {
if decimal_num == 0 {
return "0".to_string();
}
let m... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/conversions/octal_to_hexadecimal.rs | src/conversions/octal_to_hexadecimal.rs | // Author: NithinU2802
// Octal to Hexadecimal Converter: Converts Octal to Hexadecimal
// Wikipedia References:
// 1. https://en.wikipedia.org/wiki/Octal
// 2. https://en.wikipedia.org/wiki/Hexadecimal
pub fn octal_to_hexadecimal(octal_str: &str) -> Result<String, &'static str> {
let octal_str = octal_str.trim();... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/conversions/binary_to_hexadecimal.rs | src/conversions/binary_to_hexadecimal.rs | // Author : cyrixninja
// Binary to Hex Converter : Converts Binary to Hexadecimal
// Wikipedia References : 1. https://en.wikipedia.org/wiki/Hexadecimal
// 2. https://en.wikipedia.org/wiki/Binary_number
static BITS_TO_HEX: &[(u8, &str)] = &[
(0b0000, "0"),
(0b0001, "1"),
(0b0010, ... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
TheAlgorithms/Rust | https://github.com/TheAlgorithms/Rust/blob/38024b01c29eb05f733d480f88f19f0c06922a85/src/conversions/binary_to_decimal.rs | src/conversions/binary_to_decimal.rs | use num_traits::CheckedAdd;
pub fn binary_to_decimal(binary: &str) -> Option<u128> {
if binary.len() > 128 {
return None;
}
let mut num = 0;
let mut idx_val = 1;
for bit in binary.chars().rev() {
match bit {
'1' => {
if let Some(sum) = num.checked_add(&id... | rust | MIT | 38024b01c29eb05f733d480f88f19f0c06922a85 | 2026-01-04T15:37:39.002409Z | false |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.