-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path23.rb
43 lines (37 loc) · 809 Bytes
/
23.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
class Integer
def proper_divisors_sum
return 0 if self == 1
lim = Math.sqrt(self)
divs = []
(2..lim).each do |i|
if self % i == 0
divs << i
divs << (self / i) unless i == lim #othewise sqrt shows up twice
end
end
return divs.inject(0, :+)
end
def abundant?
self.proper_divisors_sum > self
end
end
#find all abundants up to 28123
limit = 28123
ab = (12..limit).select { |n| n.abundant? }
#find all sums of those abundants
sum_ab = []
l = ab.size() -1
0.upto l do |i|
i.upto l do |j|
num = ab[i] + ab[j]
sum_ab << num unless num > limit
end
end
sum_ab.sort!
sum_ab.uniq!
class Array
def bsearch(e, l = 0, u = length - 1)
return if l>u;m=(l+u)/2;e<self[m]?u=m-1:l=m+1;e==self[m]?m:bsearch(e,l,u)
end
end
puts (1..limit).reject { |i| sum_ab.bsearch i }.inject(:+)