He explains that you should try simple things right away. I thought I will give it a try! Watch the video at the end to know more.
So what is Fibonacci series?
It is a series of numbers generated by adding a number and its previois number. That’s it. It needs two numbers to start off with.
0, 1, 1, 2, 3, 5, 8, ...
A graphical representation will produce a nice exponential, infinite curve.
This can start from any two numbers
2, 4, 6, 10, 16, 26, ...
I chose 3 and 4. Now, I will be generate first 100 Fibonacci numbers.
The data is pretty straight forward. I have two numbers, I have to add them to get the 3rd number. Then add the 3rd and 2nd number to get the 4th number and so on.
3, 4, 7, 11, 18, 29, ...
How may variables should I use? I must use 2. May be one more to hold the sum of these two.
So initially we have,
x = 3
y = 4
the sum of these
z = x+y
We got the third number in the series. Now for fourth number, we should add y and z. We can actually loop this process.
Once we get z in the last process, assign
x = y
y = z
This way the next addition
x+y is actually y+z.
So the complete code with for loop looks like
HTML because you can save it as an html file and run it to see the output.
I came up with this code because this is how I used to do it in C in college. There can be a better way to do this but I wanted to come up with my own code. Not to copy from anywhere. Let me know if there’s an easier way.
Thanks for reading!