Welcome Guest, Not a member yet? Register   Sign In
The Fibonacci sequence exceeds a certain target value.
#1

I'm attempting to construct a Fibonacci series and list each element. I'd want to specify that no element in the Fibonacci sequence should be bigger than a certain number. I wrote a function called myFib to construct this list and passed a number of 20 as a parameter, anticipating that my Fibonacci list would only contain values less than 20. My loop terminates after appending element 21, and I'm attempting to figure out what I'm doing wrong. Once I get this to work, I'd want to do it for a target value of 2000000, therefore I'm attempting to figure out the mechanism before setting a huge target amount.
Code:
def myFib(max_count):
    a = 1
    b = 1
    fiblist = [1, 1]
    while fiblist[-1] <= max_count:
        temp = a
        a = b
        b = temp + a
        fiblist.append(b)
        print(fiblist)
    return fiblist

myFib(20)

I'm receiving the following output:
Code:
[1, 1, 2, 3, 5, 8, 13, 21]

Could someone assist me with this issue? According to this article that I've read, you should add a condition in the code that breaks the while loop so that no value exceeds the max count variable. Is that right?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB