add 55
This commit is contained in:
@@ -0,0 +1,10 @@
|
|||||||
|
[package]
|
||||||
|
name = "euler55"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
type Int = u128;
|
||||||
|
|
||||||
|
const MAX_VALUE: Int = 10_000;
|
||||||
|
const NUMBER_CASES: Int = MAX_VALUE - 1;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut lychrel_count = NUMBER_CASES;
|
||||||
|
let numbers: Vec<_> = (1..MAX_VALUE).into_iter().collect();
|
||||||
|
for n in numbers {
|
||||||
|
let mut accum = n;
|
||||||
|
for _ in 0..50 {
|
||||||
|
accum += reverse_number(accum);
|
||||||
|
if is_palindrome(accum) {
|
||||||
|
lychrel_count -= 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("{lychrel_count}");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reverse_number(x: Int) -> Int {
|
||||||
|
x.to_string()
|
||||||
|
.chars()
|
||||||
|
.rev()
|
||||||
|
.collect::<String>()
|
||||||
|
.parse()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_palindrome(x: Int) -> bool {
|
||||||
|
let c1 = x.to_string();
|
||||||
|
let c2 = c1.chars().rev().collect::<String>();
|
||||||
|
c1 == c2
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use crate::{is_palindrome, reverse_number};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_palindrome() {
|
||||||
|
assert!(is_palindrome(121));
|
||||||
|
assert!(!is_palindrome(120));
|
||||||
|
assert!(is_palindrome(123454321));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_reverse_number() {
|
||||||
|
assert_eq!(reverse_number(12345), 54321);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_iteration() {
|
||||||
|
let mut accum = 349;
|
||||||
|
for _ in 0..3 {
|
||||||
|
accum += reverse_number(accum);
|
||||||
|
}
|
||||||
|
assert_eq!(accum, 7337);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_196() {
|
||||||
|
let mut accum = 196;
|
||||||
|
for _ in 0..50 {
|
||||||
|
accum += reverse_number(accum);
|
||||||
|
}
|
||||||
|
assert!(!is_palindrome(accum));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user