-
Notifications
You must be signed in to change notification settings - Fork 0
/
e95.rb
51 lines (46 loc) · 1.31 KB
/
e95.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
def divisorsum(x)
ret = 1
(2..x).each {|i|
if (i * i > x)
break
end
if (x % i == 0)
ret += i + (x / i)
end
}
return ret
end
amChainLength = Array.new(size = 1000001, obj = 0)
maxCLength = 0
ans = 0
(4..1000000).each {|i|
chain = [i]
nextElem = i
while (nextElem < 1000000 and amChainLength[nextElem] == 0)
nextElem = divisorsum(nextElem)
if (chain.include? nextElem)
atIndex = chain.index(nextElem)
(0..(atIndex - 1)).each{ |ind|
amChainLength[chain[ind]] = 1
}
subl = chain.length - atIndex
minElem = nextElem
(atIndex..(chain.length - 1)).each{ |ind|
amChainLength[chain[ind]] = 1
minElem = [minElem, chain[ind]].min
}
if (subl > maxCLength)
maxCLength = subl
ans = minElem
end
elsif (nextElem > 1000000)
chain.each {|elem| amChainLength[elem] = 1}
end
chain <<= nextElem
end
# If we hit something that made us end prematurely:
if (amChainLength[i] == 0)
chain.each {|elem| amChainLength[elem] = 1}
end
}
printf("%d\n", ans)