-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprob4.lua
96 lines (79 loc) · 1.88 KB
/
prob4.lua
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
--[[
ProjectEuler.net
Solution to Problem 4
by Neil Sainsbury
6th December, 2011
--]]
-- next palindrome down from 998,001 (999x999)
local digits = {9, 9, 7, 7, 9, 9}
local pivot = 3
local numDigits = 6
local evenDigits = true
local function getDecimalValue(digits)
local value = 0
local n = #digits
for i = 1, n do
value = value + digits[i] * (10^(n-i))
end
return value
end
-- Truly horrible implementation…but it works! :-)
local function getNextPalindrome()
-- Reduce value at pivot by 1
if (digits[pivot] == 0) then
if (digits[pivot - 1] == 0) then
if (digits[1] == 1) then
table.remove(digits, 1);
digits[1] = 9
digits[2] = 9
numDigits = numDigits - 1
evenDigits = (numDigits % 2 == 0)
else
digits[1] = digits[1] - 1
digits[pivot - 1] = 9
end
else
digits[pivot - 1] = digits[pivot - 1] - 1
end
digits[pivot] = 9
else
digits[pivot] = digits[pivot] - 1
end
-- now build up the mirror around the pivot
if (evenDigits) then
local x = 0
for i = pivot + 1, numDigits do
digits[i] = digits[pivot - x]
x = x + 1
end
else
for i = 1, numDigits - pivot do
digits[pivot + i] = digits[pivot - i]
end
end
local decimalVal = getDecimalValue(digits)
-- if number is below 10,000 (100x100)
if (decimalVal < 10000) then
return nil
else
return decimalVal
end
end
local palindrome = getDecimalValue(digits)
while (palindrome) do
local math_floor = math.floor
local math_max = math.max
local math_sqrt = math.sqrt
-- is the current palindrome, N, a product of two 3-digit numbers
-- search between N/999 and sqrt(N)
local minFactor = math_floor(palindrome / 999)
local min = math_max(100, minFactor)
local max = math_sqrt(palindrome)
for n = min, max do
if (palindrome % n == 0) then
print("The largest palindrome is "..palindrome)
os.exit()
end
end
palindrome = getNextPalindrome()
end