-
Notifications
You must be signed in to change notification settings - Fork 0
/
prob021.rb
47 lines (41 loc) · 811 Bytes
/
prob021.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
def listfactors( value )
list = []
factor = 2
while factor <= Math.sqrt(value) do
if( value % factor == 0 )
list << factor
value /= factor
else
factor = factor + 1
end
end
list << value
return list
end
def listdivisors( value )
divisors = [1,value]
factors = listfactors(value)
n = factors.count - 1
while n > 0 do
factors.combination(n){ |combo|
divisors << combo.reduce(:*)
}
n -= 1
end
return divisors.uniq
end
def sumofdivisors( n )
listdivisors(n).reduce(:+) - n
end
result = 0
max = 10000
sum_of_divisors = [0, 1] + (2..max).map{ |n| sumofdivisors(n) }
sum_of_divisors.each_with_index { |sum, n|
if( sum != n )
if( ( sum <= max and sum_of_divisors[sum] == n ) or ( sum > max and sumofdivisors(sum) == n ) )
p [n, sum]
result += n
end
end
}
p result