Newbie: OverflowError: integer addition - Python data types??

Remco Gerlich scarblac-spamtrap at pino.selwerd.nl
Thu Feb 3 11:21:54 CET 2000


e.e.sutton at cummins.com wrote in comp.lang.python:
> def Fibonacci(i):
>    fibA=1
>    fibB=1
>    if i == 0:
>       Fibonacci = fibA
>    elif i == 1:
>       Fibonacci = fibB
>    elif i > 1:
>       fib=0
>       for j in range(2, i+1):
>          fib = fibA + fibB
>          fibA = fibB
>          fibB = fib
>       Fibonacci = fib
>    return Fibonacci
> 
> Thanks in advance for any tips or suggestions

Personally I like this version best:

def Fibonacci(n):
    if n < 0:
        raise "ArgumentError", "Argument must be non-negative"
    if n < 2:
        return 1  # Fibonacci(0) == 1, Fibonacci(1) == 1

    # n >= 2
    fibA = fibB = 1L
    for i in range(2, n+1):
        fib = fibA + fibB
        fibA = fibB
        fibB = fib
    return fib

It seems you like to declare your variables first, and that you used
a language that returns things with "Functionname = value". Pascal?

-- 
Remco Gerlich,  scarblac at pino.selwerd.nl
  Murphy's Rules, "Stopwatch battles":
   Each turn in Star Fleet Battles works out to one-thirtieth of a
   second. During this time, a ship can maneuver, fire all weapons, and
   send out a boarding party.



More information about the Python-list mailing list