どこぞの誰かさんが・・・
フィボナッチ数列だけじゃなく、トリボナッチ数列やテトラナッチ数列にも対応したものを作れるかな〜?と言っていたので、、、
フィボナッチ数列をトリボナッチ数列に変えたもの
answer,a,b,c,limit = 0,0,0,1,4000000 while (a <= limit): if((a % 2) == 0): answer += a a,b,c = b,c,a+b+c answer
フィボナッチ数列をテトラナッチ数列に変えたもの
answer,a,b,c,d,limit = 0,0,0,0,1,4000000 while (a <= limit): if((a % 2) == 0): answer += a a,b,c,d = b,c,d,a+b+c+d answer
これを送りつけたら、、、、
どれでも対応できるメソッドつくりやがれこのやろうと言われたので書いてみた。
ものすごい適当だし、汚いけどまぁいいや。。。
適当に初期値の配列と上限を入れれば動くだろう。
上限を4000000にした時に、上記の二つの答えと同じになったので大丈夫だろう。。。
def hogenatti(seed): import copy temp1 = copy.copy(seed) while True: yield temp1[0] temp2 = copy.copy(temp1) for j in range(len(temp1)): if(j == (len(temp1) - 1)): a = 0 for k in range(len(temp1)): a += temp2[k] temp1[j] = a else: temp1[j] = temp2[j + 1] def calc(seed,limit): answer = 0 natti = hogenatti(seed) while True: temp = natti.next() if(temp <= limit): if((temp % 2) == 0): answer += temp else: return answer seed_fib = [0,1] seed_tri = [0,0,1] seed_tet = [0,0,0,1] limit = 10000 ** 10000 calc(seed_fib,limit) calc(seed_tri,limit) calc(seed_tet,limit)