我的Fibonacci序列发生器出了什么问题？What's wrong with my Fibonacci sequence generator?

``````def fibonacci(length):
fibs = [0,1]
while length > len(fibs):
fibs.append(fibs[-1] + fibs[-2])
return fibs

fibs = fibonacci(5000)

for i in fibs:
if len(str(i)) > 1000:
print i

## The location of the number in the Fibonacci set.
print [j for j, x in enumerate(fibs) if x == i]
``````

``````11867216745258291596767088485966669273798582100095758927648586619975930687764095025968215177396570693265703962438125699711941059562545194266075961811883693134762216371218311196004424123489176045121333888565534924242378605373120526670329845322631737678903926970677861161240351447136066048164999599442542656514905088616976279305745609791746515632977790194938965236778055329967326038544356209745856855159058933476416258769264398373862584107011986781891656652294354303384242672408623790331963965457196174228574314820977014549061641307451101774166736940218594168337251710513138183086237827524393177246011800953414994670315197696419455768988692973700193372678236023166645886460311356376355559165284374295661676047742503016358708348137445254264644759334748027290043966390891843744407845769620260120918661264249498568399416752809338209739872047617689422485537053988895817801983866648336679027270843804302586168051835624516823216354234081479331553304809262608491851078404280454207286577699580222132259241827433
``````

6 个回复6

===============>>#2 票数：2

``````   import decimal
def check_fib(n):
a, b = decimal.Decimal(5*(n**2) + 4), decimal.Decimal(5*(n**2) - 4)
return any(int(x.sqrt())==x for x in (a, b))
``````

===============>>#3 票数：1

``````def fibonacci(length):
fibs = [0,1]
while length > len(fibs):
fibs.append(fibs[-1] + fibs[-2])
return fibs

fibs = fibonacci(5000)
for i,n in enumerate(fibs):

if len(str(n)) >= 1000:
print i
print n
break
``````

``````def fib():
x, y = 0, 1
while True:
yield x
x += y
x, y = y, x
f = fib()
for i,n in enumerate(f):
if len(str(n)) >= 1000:
print i
print n
break
``````

===============>>#4 票数：1

``````from decimal import Decimal
from math import sqrt

#sqrt_5 = Decimal(sqrt(5))
sqrt_5 = decimal.Decimal(5).sqrt() # As thkang suggested!
fib = lambda n: (1/sqrt_5)*( (2/(-1+ sqrt_5))**(n+1) - (2/(-1-sqrt_5))**(n+1))

for i in xrange(10000):
print i+1
``````

4782是我的代码的第一个1000位数字。

===============>>#5 票数：0

log_10（F_N）> = 999

log_10（（（1 + SQRT（5））/ 2）^ N / SQRT（5））> = 999

N> =（999 + log_10（SQRT（5）））/ log_10（（1 + SQRT（5））/ 2）〜= 4781.8592

===============>>#6 票数：0

``````from decimal import Decimal
from math import sqrt

while True:
#sqrt_5 = Decimal(sqrt(5))
sqrt_5 = Decimal(5).sqrt() # As thkang suggested!
fib = lambda n: (1/sqrt_5)*( (2/(-1+ sqrt_5))**(n) - (2/(-1-sqrt_5))**(n))
a=input()
if a=="x":
break
d=round(fib(int(a)))

print("\t"+str(d))
``````