Finish amicable pairs program
This commit is contained in:
parent
a3d191944a
commit
4210b24678
|
@ -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]
|
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue