From 67d5e4e5f2fc556822c252a7ebe1ed9aa65aa668 Mon Sep 17 00:00:00 2001 From: Simon Green Date: Mon, 31 Jan 2022 09:18:32 +1100 Subject: sgreen solutions to challenge 149 --- challenge-149/sgreen/python/ch-1.py | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100755 challenge-149/sgreen/python/ch-1.py (limited to 'challenge-149/sgreen/python/ch-1.py') diff --git a/challenge-149/sgreen/python/ch-1.py b/challenge-149/sgreen/python/ch-1.py new file mode 100755 index 0000000000..a192f81016 --- /dev/null +++ b/challenge-149/sgreen/python/ch-1.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +import sys + +fibs = [0, 1] + + +def is_sum_fib(n): + global fibs + + # Make sure we have all fibonacci numbers <= n + while (fibs[-1] < n): + fibs.append(fibs[-2] + fibs[-1]) + + return True if n in fibs else False + + +def get_sum(n): + # Get the sum of all the digits + sum = 0 + for digit in str(n): + sum += int(digit) + return sum + + +def main(n): + value = 0 + solutions = [] + + # Keep going until we have n solutions. + while len(solutions) < n: + # If the sum of this number is a fibannoci number, add it + if is_sum_fib(get_sum(value)): + solutions.append(value) + value += 1 + + print(*solutions, sep=', ') + + +if __name__ == '__main__': + main(int(sys.argv[1])) -- cgit