nbonacci sequence (nボナッチ数列)の計算及び出力のためのPython3のコード例。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# See https://sidestory.pandanote.info/nbonacci.html for details. | |
# | |
import sys | |
import re | |
if (len(sys.argv) < 3 or re.match(r'^[1-9]\d*$',sys.argv[1]) == None or re.match(r'^[1-9]\d*$',sys.argv[2]) == None): | |
print("Usage: nbonacci.py <number of terms to compute the next term> <number of terms to calculate>") | |
sys.exit(1) | |
ac = int(sys.argv[1]) | |
an = int(sys.argv[2]) | |
a = [0] * (ac-1) | |
a.append(1) | |
i = 0 | |
while (i < an): | |
if i < ac: | |
print("{0:d}".format(a[i])) | |
else: | |
next = sum(a) | |
print("{0:d}".format(next)) | |
a.pop(0) | |
a.append(next) | |
i += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment