Finished PE 092 with brute force, not hard
This commit is contained in:
8
projecteuler/092/Cargo.toml
Normal file
8
projecteuler/092/Cargo.toml
Normal 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]
|
||||||
42
projecteuler/092/src/main.rs
Normal file
42
projecteuler/092/src/main.rs
Normal 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user