Fibonacci: returning a selection of the series

Baba raoulbia at
Fri Sep 3 00:25:11 CEST 2010

On Aug 29, 7:18 pm, Alain Ketterlin <al... at>

> In general, if you have a program that produces something just to
> remove/ignore it five lines later, you have a problem. In your case:
> 1) are you sure you need to append to list(*) at every iteration? When
> do you *really* need to? And...
> 2) your loop runs up to n (the index of the fib number), but you want to
> stop on some fib number value (not index).
> So, why not pass start and end to i_fib, and use them where appropriate?

Hi Alain

Thank you for your (pragmatic) suggestions! Based on your input i was
able to considerably optimise my approach:

def fib_range(start, end):
    fib_1 = 0
    fib_2 = 1
    range = []
    while fib_2 < end:
        fib_1, fib_2 = fib_2, fib_1 + fib_2
        if fib_2 >= start and fib_2 <= end:
    return range

print fib_range(4,76)


More information about the Python-list mailing list