7 releases (3 stable)

1.0.2 Apr 23, 2024
1.0.1 Apr 18, 2024
1.0.0 Oct 30, 2023
0.0.4 Jul 12, 2023

#1 in #reversed

MIT/Apache

11KB
78 lines

Crates.io MIT licensed Apache 2.0 licensed Contributor Covenant

Number palindromes

A palindrome is a number that is the same when the digits are reversed. For example, 121, 2332, and 6 are all palindromes. But 10 is not a palindrome (since leading zeroes are not allowed). 0 is treated as a palindrome.

To check if a number is a palindrome, use is_palindrome function, e.g.:

 let x = 123; // no, this is not a palindrome
 let is_palindrome = palindronum::is_palindrome(x);
 println!("{x} is a palindrome: {is_palindrome}");

output:

 123 is a palindrome: false
 let x = 121; // yes, this is a palindrome
 let is_palindrome = palindronum::is_palindrome(x);
 println!("{x} is a palindrome: {is_palindrome}");

output:

 121 is a palindrome: true

To generate first n palindromes, use first_n_palindromes function, e.g.:

let first_10_palindromes = palindronum::first_n_palindromes(10);
for x in first_10_palindromes {
  println!("{x:2} is a palindrome");
}

output:

  1 is a palindrome
  2 is a palindrome
  3 is a palindrome
  4 is a palindrome
  5 is a palindrome
  6 is a palindrome
  7 is a palindrome
  8 is a palindrome
  9 is a palindrome
 11 is a palindrome

License

Licensed under either of

at your option.

Contribution

Any contributions to palindronum are greatly appreciated. All contributions intentionally submitted for inclusion in the work by you, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps