fixed 21
This commit is contained in:
parent
f51b9ad41a
commit
4a5f756db6
@ -1,4 +1,4 @@
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::time::Instant;
|
||||
|
||||
fn main() {
|
||||
@ -6,40 +6,38 @@ fn main() {
|
||||
let mut amicable_cache: HashMap<isize, isize> = HashMap::new();
|
||||
let mut despicable_cache: HashMap<isize, isize> = HashMap::new();
|
||||
let mut sum_amicable = 0;
|
||||
for i in 2..=10 {
|
||||
if amicable_cache.get(&i).is_some() {
|
||||
continue;
|
||||
}
|
||||
if despicable_cache.get(&i).is_some() {
|
||||
continue;
|
||||
}
|
||||
for i in 2..10000 {
|
||||
let divisors = get_divisors(i);
|
||||
let sum_divisors = divisors.iter().sum();
|
||||
if amicable_cache.get(&sum_divisors).is_some() {
|
||||
continue;
|
||||
}
|
||||
if despicable_cache.get(&sum_divisors).is_some() {
|
||||
continue;
|
||||
}
|
||||
let other_divisors = get_divisors(sum_divisors);
|
||||
let other_sum_divisors = other_divisors.iter().sum();
|
||||
if sum_divisors == other_sum_divisors {
|
||||
println!("{:?}", divisors);
|
||||
if i == other_sum_divisors && i != sum_divisors {
|
||||
amicable_cache.insert(other_sum_divisors, sum_divisors);
|
||||
sum_amicable += sum_divisors + other_sum_divisors;
|
||||
} else {
|
||||
println!("{:?}", divisors);
|
||||
despicable_cache.insert(other_sum_divisors, sum_divisors);
|
||||
}
|
||||
}
|
||||
println!("{:?}", amicable_cache);
|
||||
println!("{sum_amicable}");
|
||||
println!("cache: {:?}", amicable_cache);
|
||||
println!("sum: {sum_amicable}");
|
||||
println!("Time={} µs", now.elapsed().as_micros());
|
||||
}
|
||||
|
||||
fn get_divisors(n: isize) -> Vec<isize> {
|
||||
let mut divisors = vec![1];
|
||||
let mut divisors = HashSet::from([1]);
|
||||
let mut potential_divisor = 2;
|
||||
while (potential_divisor * potential_divisor) < n {
|
||||
while (potential_divisor * potential_divisor) <= n {
|
||||
if n % potential_divisor == 0 {
|
||||
divisors.push(potential_divisor);
|
||||
divisors.push(n / potential_divisor);
|
||||
divisors.insert(potential_divisor);
|
||||
divisors.insert(n / potential_divisor);
|
||||
}
|
||||
potential_divisor = potential_divisor + 1;
|
||||
}
|
||||
divisors
|
||||
divisors.iter().map(|&divisor| divisor).collect()
|
||||
}
|
Loading…
Reference in New Issue
Block a user