palindromeda

Crates.iopalindromeda
lib.rspalindromeda
version
sourcesrc
created_at2024-12-07 08:12:23.787846
updated_at2024-12-11 16:34:04.514874
descriptionPalindrome number generator and checker at blazing speed
homepage
repositoryhttps://github.com/CaelusV/palindromeda
max_upload_size
id1475431
Cargo.toml error:TOML parse error at line 17, column 1 | 17 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
(CaelusV)

documentation

https://docs.rs/palindromeda

README

Crates.io Docs.rs MIT License

Palindrome generator and checker for numbers.

A palindrome is a letter, number or any other sequence that is the exact same forwards and backwards. This crate is specifically for palindromic numbers.

Checking for palindromes

If you want to check whether an unsigned integer is a palindrome, use the is_palindrome function:

let pal1: u64 = 8008; // This is a palindrome.
println!("Is {pal1} a palindrome? {}", pal1.is_palindrome());

let pal2: u8 = 69; // This is NOT a palindrome.
println!("Is {pal2} a palindrome? {}", pal2.is_palindrome());

Output:

Is 8008 a palindrome? true
Is 69 a palindrome? false

Generating palindromes

Generating a palindrome is as easy as using either Palindrome::le, Palindrome::ge or Palindrome::closest for the nearest palindrome to a number, or by retrieving it based on its palindrome-index with Palindrome::nth:

use palindromeda::Palindrome;

let number1: u64 = 420; // This number is too high.
// Let's get a palindrome that's lower.
println!("Palindrome that's lower: {}", Palindrome::le(number1));

let number2: u64 = 1337;
// Let's get a palindrome that's higher.
println!("Palindrome that's higher: {}", Palindrome::ge(number2));

let number3: u64 = 5340; // Which palindrome is closest?
println!("Closest palindrome: {}", Palindrome::closest(number3));

let number4: usize = 1000; // 1001st palindrome (0-based indexing)
println!("1001st palindrome: {}", Palindrome::nth(number4).unwrap());

Output:

Palindrome that's lower: 414
Palindrome that's higher: 1441
Closest palindrome: 5335
1001st palindrome: 90109

And if you want, you can go from palindrome to palindrome with the Palindrome::previous and Palindrome::next functions.

Iterating over palindromes

With PalindromeIter you can iterate over a large swathe of palindromes. You can iterate over a custom range with PalindromeIter::from_u64 or iterate over the first n palindromes with PalindromeIter::first_n.

You can also iterate over the first n palindromes after (and including) a specific palindrome with PalindromeIter::first_n_from. Be sure to use PalindromeIter::len for quickly determining the length of the iterator.

Commit count: 41

cargo fmt