Haulien icon

Fibonacci Generator

Haulien | PRO | 10/31/11 12:35:24 AM UTC | 0 ⭐ | 308 👁️ | Never ⏰ | []
Python |

1.19 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
    
    
 
print "\n\nTime elapsed: " + str(elapsed) + "s\n\n"
 
    
 
print "\n\nGoodbye\n\n"
 
 
 
 

Comments

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

    0 B

    |

    👍

    /

    👎