Finished PE 092 with brute force, not hard

This commit is contained in:
2023-06-26 16:01:39 +02:00
parent 863f051f7d
commit 958ee4651e
2 changed files with 50 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
[package]
name = "main"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View File

@@ -0,0 +1,42 @@
use std::collections::HashSet;
use std::time::Instant;
fn square_digits(n: u32) -> u32 {
let mut result = 0;
let mut n = n;
while n > 0 {
result += (n % 10).pow(2);
n /= 10;
}
return result;
}
fn main() {
println!("Hello, this is Patrick!");
let now = Instant::now();
const MAX_INT: u32 = 10000000;
let mut loop_1 = HashSet::new();
let mut loop_89 = HashSet::new();
for n in 1..MAX_INT {
let mut m = n;
loop {
if loop_1.contains(&m) || m == 1 {
loop_1.insert(n);
break;
} else if loop_89.contains(&m) || m == 89 {
loop_89.insert(n);
break;
} else {
m = square_digits(m);
}
}
}
println!("Number of numbers that end in 89-loop: {}", loop_89.len());
println!("Time passed: {:?}", Instant::now() - now);
}