From 4210b24678fd0d52ef09804a9248c78c496e300b Mon Sep 17 00:00:00 2001 From: Bit Borealis Date: Sat, 14 May 2022 19:38:11 +0000 Subject: [PATCH] Finish amicable pairs program --- amicable/Cargo.toml | 8 ++++++++ amicable/src/main.rs | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 amicable/Cargo.toml create mode 100644 amicable/src/main.rs diff --git a/amicable/Cargo.toml b/amicable/Cargo.toml new file mode 100644 index 0000000..a4666cc --- /dev/null +++ b/amicable/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "amicable" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/amicable/src/main.rs b/amicable/src/main.rs new file mode 100644 index 0000000..7eabe86 --- /dev/null +++ b/amicable/src/main.rs @@ -0,0 +1,25 @@ +fn main() { + let mut total = 0; + for x in 1..10_000 { + let candidate = divisors_sum(x); + let c_sum = divisors_sum(candidate); + if x != candidate && x < candidate && x == c_sum { + println!("x: {}, candidate: {}, c_sum: {}", x, candidate, c_sum); + total += x+candidate; + } + } + println!("{}", total); +} + +fn divisors_sum(dividend: usize) -> usize { + let mut divisor = 2; + let mut total = 1; + while divisor * divisor < dividend { + if dividend % divisor == 0 { + total += divisor; + total += dividend / divisor; + } + divisor+=1; + } + total +}