-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathproblem-023.rb
51 lines (41 loc) · 910 Bytes
/
problem-023.rb
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby
# Find the sum of all the positive integers which cannot be written as the sum
# of two abundant numbers
def abundant_number?(number)
sum = 0
(1..Math.sqrt(number)).each do |n|
if number%n == 0
sum += n
sum += number/n
end
end
# correction for perfect squares
if Math.sqrt(number).integer?
sum -= Math.sqrt(number)
end
if sum > number
return true
else
return false
end
end
abundant_numbers = []
(1..28123).each do |number|
if abundant_number?(number)
abundant_numbers << number
end
end
abundant_sums = []
(0..abundant_numbers.size-1).each do |i|
(i..abundant_numbers.size-1).each do |j|
abundant_sums << abundant_numbers[i] + abundant_numbers[j]
end
end
numbers = []
(1..28123).each {|x| numbers << x}
numbers = numbers - abundant_sums
sum = 0
numbers.each do |number|
sum += number
end
puts "Answer: #{sum}"