-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathp95.vx
80 lines (66 loc) · 972 Bytes
/
p95.vx
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const max = 1000000
def contains(l, k)
{
for n in l do
{
if n == k then
{
return true
}
}
return false
}
def sum_divisors(n)
{
var sum = 1
var m = 2
# Note: There MUST be a line before "while" for it to work
while (m * m) <= n do
{
if (n % m) == 0 then
{
sum += m + (n / m)
}
m++
}
return sum
}
def compute_amicable_chain(m)
{
var n = m
var smallest = max
var chain_length = 1
var seen = []
while true do
{
seen <-- n
n = sum_divisors(n)
if n < smallest then
{
smallest = n
}
if n == m then
{
return chain_length, smallest
}
if (n > max) or (n < m) or contains(seen, n) then
{
return 0, max
}
chain_length++
}
}
# Entry point
var max_length = 0
var smallest_member = max
for n in 1 -> (max + 1) do
{
var (length, smallest) = compute_amicable_chain(n)
if length > max_length then
{
max_length = length
smallest_member = smallest
print smallest_member
}
}
print smallest_member