Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
* [Modular Exponential](https://github.com/TheAlgorithms/Rust/blob/master/src/math/modular_exponential.rs)
* [Newton Raphson](https://github.com/TheAlgorithms/Rust/blob/master/src/math/newton_raphson.rs)
* [Nthprime](https://github.com/TheAlgorithms/Rust/blob/master/src/math/nthprime.rs)
* [Palindrome Number](https://github.com/TheAlgorithms/Rust/blob/master/src/math/palindrome.rs)
* [Pascal Triangle](https://github.com/TheAlgorithms/Rust/blob/master/src/math/pascal_triangle.rs)
* [Perfect Cube](https://github.com/TheAlgorithms/Rust/blob/master/src/math/perfect_cube.rs)
* [Perfect Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/perfect_numbers.rs)
Expand Down
2 changes: 2 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ mod miller_rabin;
mod modular_exponential;
mod newton_raphson;
mod nthprime;
mod palindrome;
mod pascal_triangle;
mod perfect_cube;
mod perfect_numbers;
Expand Down Expand Up @@ -142,6 +143,7 @@ pub use self::miller_rabin::{big_miller_rabin, miller_rabin};
pub use self::modular_exponential::{mod_inverse, modular_exponential};
pub use self::newton_raphson::find_root;
pub use self::nthprime::nthprime;
pub use self::palindrome::is_palindrome;
pub use self::pascal_triangle::pascal_triangle;
pub use self::perfect_cube::perfect_cube_binary_search;
pub use self::perfect_numbers::perfect_numbers;
Expand Down
69 changes: 69 additions & 0 deletions src/math/palindrome.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/// A palindrome number is a number that reads the same backward as forward.
/// For example, 121 is a palindrome, but 123 is not.
/// This function checks if a given unsigned 64-bit integer 'number' is a palindrome
/// by mathematically reversing its digits and comparing it to the original.
/// Note: By this definition, negative numbers are not considered palindromes.

pub fn is_palindrome(number: u64) -> bool {
// A single-digit number is always a palindrome
if number < 10 {
return true;
}

let original_number = number;
let mut reversed_number: u64 = 0;
let mut n = number;

// Loop until all digits of n have been processed
while n > 0 {
// Get the last digit
let remainder = n % 10;

// Build the reversed number
reversed_number = (reversed_number * 10) + remainder;

// Remove the last digit
n /= 10;
}

// Check if the original number equals its reversed version
original_number == reversed_number
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn standard_palindrome() {
assert!(is_palindrome(121));
}

#[test]
fn standard_non_palindrome() {
assert!(!is_palindrome(123));
}

#[test]
fn single_digit() {
// Single digits are always palindromes
assert!(is_palindrome(7));
}

#[test]
fn zero() {
// Zero is a palindrome
assert!(is_palindrome(0));
}

#[test]
fn large_palindrome() {
assert!(is_palindrome(123454321));
}

#[test]
fn number_ending_in_zero() {
// No number > 0 that ends in 0 can be a palindrome
assert!(!is_palindrome(120));
}
}