2016-03-31 16 views
2

Fibonacci veya lucas sayılarında herhangi bir sayıda eleman üretmek için Python'da bir program geliştirdim (fibonacci 1,1,2,3,5,8,13 vs ve Lucas, 2,1,3,4,7,11,18 olmak üzere), fakat belli bir sayı için (belli bir eşiğin üzerinde görünen) koştuğumda, bir sebepten dolayı, bir L harfi ile takip edilen sayıyı çıkardı. İlk sayı Bunun için 2971215073 oldu, ancak diğer programlar sorun olmadan çok daha yüksek sayıları üretir. Bunu yapmayı nasıl engellerim? bütünüyle kodu: çıkışPython programı çıktı numaraları 'L' ile devam ediyor

import time 
f = [1,1] 
l = [2,1] 
x = True 
while x == True: 
    b = raw_input('Would you like to generate fibonacci numbers (enter f) or lucas numbers (enter l)?\n') 
    if b == 'l' or b == 'L' or b == 'lucas' or b == 'Lucas': 
     z = True 
     x = False 
    elif b == 'f' or b == 'F' or b == 'fibonnaci' or b == 'Fibonnaci' or b == 'Fibonacci' or b == 'fibonacci': 
     z = False 
     x = False 
    else: 
     print 'That isn\'t an answer. Enter f or l.' 
y = True 
while y == True: 
    c = raw_input('Would you like to generate up to a limit (enter a) or a certain number of elements (enter b)?\n') 
    if c == 'A' or c == 'a': 
     w = True 
     y = False 
    elif c == 'B' or 'b': 
     w = False 
     y = False 
    else: 
     print 'That isn\'t an answer. Enter a or b.' 
a = input('Enter an upper bound.\n') 
c = 1 
if z == True and w == True: 
    start = time.clock() 
    while((l[c] + l[c-1]) <= a): 
     l += [(l[c] + l[c-1])] 
     c += 1 
    print l 
    if (time.clock() - start) >= 60: 
     print 'Program took ' + str(time.clock() - start) + ' seconds to run.' 
if z == True and w == False: 
    start = time.clock() 
    while(c-1 <= a-3): 
     l += [(l[c] + l[c-1])] 
     c += 1 
    print l 
    if (time.clock() - start) >= 60: 
     print 'Program took ' + str(time.clock() - start) + ' seconds to run.' 
if z == False and w == True: 
    start = time.clock() 
    while((f[c] + f[c-1]) <= a): 
     f += [(f[c] + f[c-1])] 
     c += 1 
    print f 
    if (time.clock() - start) >= 60: 
     print 'Program took ' + str(time.clock() - start) + ' seconds to run.' 
if z == False and w == False: 
    start = time.clock() 
    while(c-1 <= a-3): 
     f += [(f[c] + f[c-1])] 
     c += 1 
    print f 
    if (time.clock() - start) >= 60: 
     print 'Program took ' + str(time.clock() - start) + ' seoonds to run.' 
raw_input('Press enter to exit.') 

Bir örnek:

Would you like to generate fibonacci numbers (enter f) or lucas numbers (enter l)? 
f 
Would you like to generate up to a limit (enter a) or a certain number of elements (enter b)? 
b 
Enter an upper bound. 
100 



    [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 
2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 
    514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 
    24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170, 1836311903, 2971215073L, 4807526976L, 7778742049L, 
    12586269025L, 20365011074L, 32951280099L, 53316291173L, 86267571272L, 
    139583862445L, 225851433717L, 365435296162L, 591286729879L, 956722026041L, 1548008755920L, 2504730781961L, 4052739537881L, 6557470319842L, 
    10610209857723L, 17167680177565L, 27777890035288L, 44945570212853L, 
    72723460248141L, 117669030460994L, 190392490709135L, 308061521170129L, 
    498454011879264L, 806515533049393L, 1304969544928657L, 2111485077978050L, 
    3416454622906707L, 5527939700884757L, 8944394323791464L, 14472334024676221L, 
    23416728348467685L, 37889062373143906L, 61305790721611591L, 
    99194853094755497L, 160500643816367088L, 259695496911122585L, 420196140727489673L, 679891637638612258L, 1100087778366101931L, 1779979416004714189L, 2880067194370816120L, 4660046610375530309L, 
    7540113804746346429L, 12200160415121876738L, 19740274219868223167L, 
    31940434634990099905L, 51680708854858323072L, 83621143489848422977L, 
    135301852344706746049L, 218922995834555169026L, 354224848179261915075L] 
     Press enter to exit. 
+0

L araçları 'uzun':

print l 

print ", ".join(str(x) for x in l) 

yine köşeli parantez istiyorsanız

ile, o zaman biçimlendirmeyi kullanabilirsiniz. Sayılar çok yüksek. Int, 32 bit hassasiyetle kodlanırken, uzunlar sınırsızdır. – loutre

+0

Python 3'e geçmeyi öneririm; Bu karışıklık artık orada yok. –

cevap

5

L "uzun tamsayı" anlamına Python 2'nin bir cilvesi. Numaraları tek tek basarsanız (listeyi yazdırmak yerine) "L" olmaz. Başka bir seçenek, bu tuhaflıkla sonuçlanan Python 3'e yükseltmek olacaktır.

değiştirmeyi düşünün:

print "[{}]".format(", ".join(str(x) for x in l)) 
+0

Onları hala bir listede tutarken onlardan kurtulmanın bir yolu var mı? – Mathime

+0

@Mathime Numaraları listede hala tutabilirsiniz. L listede yok. Listeyi liste halinde basmanın bir eseridir. Cevabımı, listeyi yazdırmanın başka bir yolu ile güncelleyeceğim. –

+0

Bu sonuç verir: TypeError: sequence item 0: beklenen dize, int – Mathime

İlgili konular