Haulien icon

Fibonacci Generator

Haulien | PRO | 10/31/11 05:05:19 AM UTC | 0 ⭐ | 385 👁️ | Never ⏰ | []
Python |

1.49 KB

|

None

|

0 👍

/

0 👎

 
import time
 
def fib(n):
    try:
        
        if n < 2:
            return n
        prev = 1
        fib = 1
        for num in range(2, n):
            prev, fib = fib, fib + prev
            #print str(fib) + ", " #remove the hash at the start of this if you want to print each number after generation
        
        print "\n\nFibonacci " + str(n) + ": \n\n" + str(fib)
        
        print "\n\nAmount of digits: " + str(len(str(fib)))
        
        
    except KeyboardInterrupt:
        print ""        #just used to catch ctrl+c (terminate) on unix-systems
 
 
 
 
try:
 
    howMany = long(raw_input("How many fibonacci numbers would you like generated?\nA Max of 100000 is recommended!\nAnything larger can freeze your system until it is generated\n\nInput your number: "))
 
    start = time.time()
    fib(howMany)
    elapsed = (time.time() - start)
    
except ValueError:
    print "\n\nPlease enter a number\n\n"
except KeyboardInterrupt:
    print ""
    #just used to catch ctrl+c (terminate) on unix-systems
    
 
minutes = elapsed/60
hours = minutes/60
days = hours/24
weeks = days/7
years = days/365
 
print "\n\nTime elapsed (seconds): " + str(elapsed) + "s"
print "Time elapsed (mins): " + str((minutes)) + "m"
print "Time elapsed (hours): " + str((hours)) + "h"
print "Time elapsed (days): " + str((days)) + "d"
print "Time elapsed (weeks): " + str((weeks)) + "w"
print "Time elapsed (years): " + str((years)) + "y\n\n"
 
 
print "\n\nGoodbye\n\n"
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎