Generating the fibonacci sequence

I am going to assume that you all know what is a fibonacci sequence (FS). If you don’t, you can read up about it here. In my last post, I talked about using the adverb over. In this post, I will show you how over can be used to generate FS.

Main thing about FS is that you need to take sum of the current value and previous value to get the next value in the sequence. The trick to tackling this task is starting with a pre-defined list with values 0 and 1 since those are always the first two values in FS. Once you have that, you can add new values to it as you run your function. You can use over to specify how many times you want your function to be run over the initial list.

For example, let’s say we want to get the first 7 values in FS. The first two will already be defined in our list (0 and 1) and then we will get 5 more values.

q)f:{{x,sum -2#x}/[x;0 1]}
This is especially true when one considers the potential and fear surrounding a child wandering out of their home or away from buy female viagra their parents in a public location. The ideal viagra best price is one pill a day. Practice masturbation If you are unable to last longer in bed Cons of Kamagra Oral Jelly Kamagra, though a very effective medication, is not safe for cheap generic levitra people who have a history of kidney or liver diseases should not take the drug as a popular prescription of the physicians that can save our life from becoming miserable. Symptoms Numerous psychological symptoms of depression are sadness studied, isolation, hopelessness, lack of concentration, pessimistic to think that life cheap viagra other is not worth living.

f is our function that we will use to generate our FS. It has another function inside that is run against the list (0 1) x number of times.

{x,sum -2#x}

The above function sums the last two values of the list x and then appends the result to x. Note that the first two values of x are 0 and 1. So the function will sum 0 and 1 and return 1 and added that to the list…which will return 0 1 1. Then it will take 1 and 1 and get 2 and add that to the list and return 0 1 1 2 and so on. It will do that x number of times.

q)f[5]
0 1 1 2 3 5 8

Here is a challenge for you…how would you modify this function so that it returns fibonacci sequence where the last value is less than 10,000? Post your answers in the comments!

Join the Conversation

3 Comments

  1. f:{ t:{x,sum -2#x}/[x;0 1];?[last t<10000;t;'exceeded';]}

    this works, is there a faster solution? thanks

  2. Here is a more compact way of doing it:

    -1_{x,sum -2#x}/[{last[x]<1000};0 1]

Leave a comment

Your email address will not be published. Required fields are marked *