From f6f8fa27a551ad1171389cc9ca8b5c9a0e39e025 Mon Sep 17 00:00:00 2001 From: Abigail Date: Tue, 25 Jan 2022 13:09:57 +0100 Subject: Week 149, part 1: More solutions bc, C, Go, Java, Lua, Node.js, Pascal, Python, R, Ruby, Scheme, and Tcl. --- challenge-149/abigail/lua/ch-1.lua | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 challenge-149/abigail/lua/ch-1.lua (limited to 'challenge-149/abigail/lua/ch-1.lua') diff --git a/challenge-149/abigail/lua/ch-1.lua b/challenge-149/abigail/lua/ch-1.lua new file mode 100644 index 0000000000..ce9ef0ddb6 --- /dev/null +++ b/challenge-149/abigail/lua/ch-1.lua @@ -0,0 +1,49 @@ +#!/opt/local/bin/lua + +-- +-- See https://theweeklychallenge.org/blog/perl-weekly-challenge-149 +-- + +-- +-- Run as: lua ch-2.lua < input-file +-- + +function digit_sum (number) + local sum = 0 + while number > 0 do + sum = sum + number % 10 + number = math . floor (number / 10) + end + return (sum) +end + +fib = {} +fib_prev = 0 +fib_last = 1 +fib [fib_prev] = 1 +fib [fib_last] = 1 + +function is_fib (n) + while fib_last < n do + local t = fib_last + fib_last = fib_last + fib_prev + fib_prev = t + fib [fib_last] = 1 + end + + return fib [n] +end + + +for n in io . lines () do + n = tonumber (n) + k = 0 + while n > 0 do + if is_fib (digit_sum (k)) then + io . write (k .. " ") + n = n - 1 + end + k = k + 1 + end + print ("") +end -- cgit