diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-01-25 21:26:16 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-25 21:26:16 +0000 |
| commit | aa1198b22c0d5bbbfc41b638390fcbdb8cacab26 (patch) | |
| tree | 1f6e662a4405cabae39336aa258120895c038f1b /challenge-149/abigail/python/ch-1.py | |
| parent | 0a207042e17d673b78681b6c0865fe0cf91f9412 (diff) | |
| parent | f6f8fa27a551ad1171389cc9ca8b5c9a0e39e025 (diff) | |
| download | perlweeklychallenge-club-aa1198b22c0d5bbbfc41b638390fcbdb8cacab26.tar.gz perlweeklychallenge-club-aa1198b22c0d5bbbfc41b638390fcbdb8cacab26.tar.bz2 perlweeklychallenge-club-aa1198b22c0d5bbbfc41b638390fcbdb8cacab26.zip | |
Merge pull request #5567 from Abigail/abigail/week-149
Abigail/week 149
Diffstat (limited to 'challenge-149/abigail/python/ch-1.py')
| -rw-r--r-- | challenge-149/abigail/python/ch-1.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-149/abigail/python/ch-1.py b/challenge-149/abigail/python/ch-1.py new file mode 100644 index 0000000000..1a7f8d33ac --- /dev/null +++ b/challenge-149/abigail/python/ch-1.py @@ -0,0 +1,45 @@ +#!/usr/local/bin/python3 + +# +# See https://theweeklychallenge.org/blog/perl-weekly-challenge-149 +# + +# +# Run as: python ch-1.py < input-file +# + + +def digit_sum (number): + sum = 0 + base = 10 + while number > 0: + sum = sum + number % base + number = number // base + return sum + +fib = {} +fib_prev = 0 +fib_last = 1 +fib [fib_prev] = True; +fib [fib_last] = True; + +def is_fib (n): + global fib, fib_prev, fib_last + while fib_last < n: + t = fib_last + fib_last = fib_last + fib_prev + fib_prev = t + fib [fib_last] = True; + return n in fib + +import fileinput, sys + +for n in fileinput . input (): + n = int (n) + k = 0 + while n > 0: + if is_fib (digit_sum (k)): + sys . stdout . write (str (k) + " ") + n = n - 1 + k = k + 1 + print ("") |
