This commit is contained in:
timeshifter
2022-08-14 22:25:36 +02:00
parent 94d9d406e6
commit 7681a2621d
2 changed files with 71 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
[package]
name = "euler52"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+63
View File
@@ -0,0 +1,63 @@
use std::collections::HashMap;
const MULTIPLES_TO_CHECK: [u64; 5] = [2, 3, 4, 5, 6];
fn main() {
let mut i = 10;
'outer: loop {
i += 1;
let hm1 = get_amount_of_digits(i);
for factor in MULTIPLES_TO_CHECK {
let double = i * factor;
let hm2 = get_amount_of_digits(double);
if hm1 != hm2 {
continue 'outer;
}
}
break;
}
println!("{i}");
}
fn get_amount_of_digits(i: u64) -> HashMap<u64, usize> {
let mut result = get_empty_hashmap();
i.to_string()
.chars()
.map(|c| c.to_digit(10).unwrap() as u64)
.for_each(|d| {
result.insert(d, result[&d] + 1);
});
result
}
fn get_empty_hashmap() -> HashMap<u64, usize> {
let mut hm = HashMap::new();
for i in 0..10 {
hm.insert(i, 0);
}
hm
}
#[cfg(test)]
mod test {
use crate::{get_amount_of_digits, get_empty_hashmap};
#[test]
fn test_get_amount_of_digits() {
let mut correct = get_empty_hashmap();
correct.insert(2, 1);
correct.insert(5, 2);
correct.insert(0, 1);
assert_eq!(correct, get_amount_of_digits(5250));
}
#[test]
fn test_get_empty_hashmap() {
let hm = get_empty_hashmap();
assert_eq!(hm.len(), 10);
for (_, v) in hm {
assert_eq!(v, 0);
}
}
}