From 89b75091efcc8ab7ec62564a9dbe319cb0552eba Mon Sep 17 00:00:00 2001 From: Roger Bell_West Date: Mon, 10 Jan 2022 10:58:14 +0000 Subject: Solutions for challenge #147 --- challenge-147/roger-bell-west/lua/ch-2.lua | 41 ++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100755 challenge-147/roger-bell-west/lua/ch-2.lua (limited to 'challenge-147/roger-bell-west/lua/ch-2.lua') diff --git a/challenge-147/roger-bell-west/lua/ch-2.lua b/challenge-147/roger-bell-west/lua/ch-2.lua new file mode 100755 index 0000000000..c46defd3d4 --- /dev/null +++ b/challenge-147/roger-bell-west/lua/ch-2.lua @@ -0,0 +1,41 @@ +#! /usr/bin/lua + +function pentagon(n) + return math.floor(n*(3*n-1)/2) +end + +function pentpair() + fpent={} + rpent={} + mx=0 + a=1 + while 1 do + while mx < a do + mx = mx + 1 + table.insert(fpent,pentagon(mx)) + rpent[fpent[mx]]=mx + end + for b = 1,a do + d=fpent[a]-fpent[b] + if d < fpent[b] then + break + end + if rpent[d] ~= nil then + s=fpent[a]+fpent[b] + while s > fpent[mx] do + mx = mx + 1 + table.insert(fpent,pentagon(mx)) + rpent[fpent[mx]]=mx + end + if rpent[s] ~= nil then + print(string.format("P(%d) + P(%d) = %d + %d = %d = P(%d)",a,b,fpent[a],fpent[b],s,rpent[s])) + print(string.format("P(%d) - P(%d) = %d - %d = %d = P(%d)",a,b,fpent[a],fpent[b],d,rpent[d])) + os.exit(0) + end + end + end + a = a + 1 + end +end + +pentpair() -- cgit