uppdateras med ojämna mellanrum

19 februari 2009

Problem #25

What is the first term in the Fibonacci sequence to contain 1000 digits?
En till utmaning där vi måste hantera stora heltal.
#!/usr/bin/env python
def fibonacci():
a, b, i = 0, 1, 0
while 1:
yield a, i
a, b, i = b, a + b, i + 1

for x, i in fibonacci():
if x >= 10**(1000 - 1):
print "Answer is %d" % i
break;
Bra användningsområde för yield, det blir så lättläst kod.

Inga kommentarer:

Om mig

Sundsvall, Sweden