From 114c543cef4a0946fd1720e5506da4b9f20747c8 Mon Sep 17 00:00:00 2001 From: Nihal Patel Date: Sat, 18 Oct 2025 00:44:50 +0530 Subject: [PATCH 1/5] Add palindrome checking function in Rust Implement a function to check if a number is a palindrome. --- src/math/palindrome.rs | 69 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/math/palindrome.rs diff --git a/src/math/palindrome.rs b/src/math/palindrome.rs new file mode 100644 index 00000000000..1a2ab447ed5 --- /dev/null +++ b/src/math/palindrome.rs @@ -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_eq!(true, is_palindrome(121)); + } + + #[test] + fn standard_non_palindrome() { + assert_eq!(false, is_palindrome(123)); + } + + #[test] + fn single_digit() { + // Single digits are always palindromes + assert_eq!(true, is_palindrome(7)); + } + + #[test] + fn zero() { + // Zero is a palindrome + assert_eq!(true, is_palindrome(0)); + } + + #[test] + fn large_palindrome() { + assert_eq!(true, is_palindrome(123454321)); + } + + #[test] + fn number_ending_in_zero() { + // No number > 0 that ends in 0 can be a palindrome + assert_eq!(false, is_palindrome(120)); + } +} From 95ee4996cdb5414daf6b2fc726ce1a865838b3f8 Mon Sep 17 00:00:00 2001 From: Nihal Patel Date: Sat, 25 Oct 2025 17:06:40 +0530 Subject: [PATCH 2/5] Add Palindrome Number link to DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 564a7813807..82211be7667 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) From 6d14162b9547bc0bad1c712e4ed11d7cb4f82bb6 Mon Sep 17 00:00:00 2001 From: Nihal Patel Date: Sat, 25 Oct 2025 17:17:56 +0530 Subject: [PATCH 3/5] Update mod.rs --- src/math/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/math/mod.rs b/src/math/mod.rs index 7407465c3b0..41ef213157a 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -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; @@ -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; From 4860e64ca9770847c68bec77b56b471207d0d2a4 Mon Sep 17 00:00:00 2001 From: Nihal Patel Date: Sat, 25 Oct 2025 17:29:14 +0530 Subject: [PATCH 4/5] Fix formatting and clippy lints --- src/math/palindrome.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/math/palindrome.rs b/src/math/palindrome.rs index 1a2ab447ed5..3da9203f2f4 100644 --- a/src/math/palindrome.rs +++ b/src/math/palindrome.rs @@ -36,34 +36,34 @@ mod tests { #[test] fn standard_palindrome() { - assert_eq!(true, is_palindrome(121)); + assert!(is_palindrome(121)); } #[test] fn standard_non_palindrome() { - assert_eq!(false, is_palindrome(123)); + assert!(!is_palindrome(123)); } #[test] fn single_digit() { // Single digits are always palindromes - assert_eq!(true, is_palindrome(7)); + assert!(is_palindrome(7)); } #[test] fn zero() { // Zero is a palindrome - assert_eq!(true, is_palindrome(0)); + assert!(is_palindrome(0)); } #[test] fn large_palindrome() { - assert_eq!(true, is_palindrome(123454321)); + assert!(is_palindrome(123454321)); } #[test] fn number_ending_in_zero() { // No number > 0 that ends in 0 can be a palindrome - assert_eq!(false, is_palindrome(120)); + assert!(!is_palindrome(120)); } } From f86876c512767507daf8df15e8a8a18884c8fde8 Mon Sep 17 00:00:00 2001 From: ITZ-NIHALPATEL Date: Sat, 25 Oct 2025 20:20:12 +0530 Subject: [PATCH 5/5] Fix formatting with cargo fmt --- src/math/palindrome.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/math/palindrome.rs b/src/math/palindrome.rs index 3da9203f2f4..6ae377d64ff 100644 --- a/src/math/palindrome.rs +++ b/src/math/palindrome.rs @@ -18,10 +18,10 @@ pub fn is_palindrome(number: u64) -> bool { 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; }