Saturday, September 20, 2008

Project Euler Problem 25 Fibonacci number lengths

[root@www ~]# python twentyfive.py
4000 836
4780 999
4782 1000
Started at: Sun Sep 21 01:16:27 2008 Ended at: Sun Sep 21 01:16:27 2008
[root@www ~]# cat twentyfive.py

# -*- coding: utf-8 -*-
"""
<+ MODULE_NAME +>
Project Euler Problem Set
<+ DESCRIPTION +>
Problem 25
Solutions in Python to the problems at http://projecteuler.net

Licensed under the Creative Commons license; see http://creativecommons.org for more details. In short, this means you can freely reuse, modify and distribute this program, also commercially, for as long you provide a proper attribution.

Copyright by Jonathan Mark, jonathanmark.com/aristede.com, jmark@aristede.com

http://en.literateprograms.org/Fibonacci_numbers_%28Python%29#Quick_exact_computation_of_large_individual_Fibonacci_numbers_.28second_take.29
for fib module.

>>> print 881 + 1
882

"""
import time
starttime = time.asctime()

# for any number of digits n, there are either four or five
# fibonacci numbers containing that many digits.
# get fibonacci_LF module from literateprograms.org
import fibonacci_LF
n = 4000
l = len(str(fibonacci_LF.fib(n)))
diff = 1000 - l
print n,l
while diff > 1:
n += 4 * diff
l = len(str(fibonacci_LF.fib(n)))
diff = 1000 - l
print n,l

while diff > 0:
n += 1
l = len(str(fibonacci_LF.fib(n)))
diff = 1000 - l
print n,l


print "Started at: ", starttime, " Ended at: ",time.asctime()

if __name__ == "__main__":
import doctest
doctest.testmod()

No comments: