-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_euler_prob21.lua
More file actions
56 lines (46 loc) · 871 Bytes
/
project_euler_prob21.lua
File metadata and controls
56 lines (46 loc) · 871 Bytes
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
numsToCheck = {}
totalSum = 0
for i = 1,9999 do
numsToCheck[i] = true
end
function divisors(input)
output = {}
i = 1
numDivs = 1
for i=1,(input-1) do
if ( (input % i) == 0) then
output[numDivs] = i
numDivs = numDivs + 1
end
end
return output
end
function sum(divs)
i = 1
local temp = 0
for i =1,#divs do
temp = temp+divs[i]
end
return temp
end
function isAmicable(input)
divs = divisors(input)
temp = sum(divs)
if ( sum(divisors(temp)) == input) then
return true,temp
end
return false,0
end
for i = 1,9999 do
local num = 0
local amicable = false
if (numsToCheck[i] == false) then goto continue end
amicable,num = isAmicable(i)
if (num == i) then amicable=false end
if amicable then
numsToCheck[num] = false
totalSum = totalSum + i + num
end
::continue::
end
print(totalSum)