This commit is contained in:
Dr. Matthias Ratajczak
2023-06-20 14:53:17 +02:00
parent 1a34dfda82
commit a47d72f8cc
2 changed files with 47 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
[package]
name = "euler92"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[profile.release]
lto = "fat"
+38
View File
@@ -0,0 +1,38 @@
const MAXIMUM: usize = 10_000_000;
fn main() {
let mut arrives_at_89 = 0;
for i in 1..MAXIMUM {
let mut number = i;
loop {
let sum: usize = number
.to_digits()
.into_iter()
.map(|d| d.pow(2) as usize)
.sum();
if sum == 89 {
arrives_at_89 += 1;
break;
} else if sum == 1 {
break;
}
number = sum;
}
}
println!("{arrives_at_89}");
}
trait ToDigits {
fn to_digits(&self) -> Vec<u8>;
}
impl ToDigits for usize {
fn to_digits(&self) -> Vec<u8> {
self.to_string()
.chars()
.map(|c| c.to_digit(10).unwrap() as _)
.collect()
}
}