-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler-021
33 lines (24 loc) · 837 Bytes
/
euler-021
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def sum_of_proper_divisors(number)
return 0 if number == 1
divisor_sum = 1 # 1 is always a proper divisor
(2..Math.sqrt(number)).each do |potential_divisor|
if number % potential_divisor == 0
divisor_sum += potential_divisor
divisor_sum += number / potential_divisor unless potential_divisor == Math.sqrt(number)
end
end
divisor_sum
end
def are_amicable?(a, b)
sum_of_proper_divisors(a) == b && sum_of_proper_divisors(b) == a && a != b
end
amicable_numbers = []
(1..9999).each do |number|
potential_amicable_partner = sum_of_proper_divisors(number)
if are_amicable?(number, potential_amicable_partner)
amicable_numbers << number
end
end
sum_of_amicable_numbers = amicable_numbers.sum
puts "The sum of all amicable numbers under 10000 is #{sum_of_amicable_numbers}"
#ran in 3.14s