Finish amicable pairs program

This commit is contained in:
Bit Borealis 2022-05-14 19:38:11 +00:00
parent a3d191944a
commit 4210b24678
Signed by: theotheroracle
GPG key ID: 2D816A2DCA6E5649
2 changed files with 33 additions and 0 deletions

8
amicable/Cargo.toml Normal file
View file

@ -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]

25
amicable/src/main.rs Normal file
View file

@ -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
}