How to Write the Fibonacci Sequence

How to write the Fibonacci Sequence?

There is lots of information about the Fibonacci Sequence on wikipedia and on wolfram. A lot more than you may need. Anyway it is a good thing to learn how to use these resources to find (quickly if possible) what you need.

Write Fib sequence formula to infinite

In math, it's given in a recursive form:

fibonacci from wikipedia

In programming, infinite doesn't exist. You can use a recursive form translating the math form directly in your language, for example in Python it becomes:

def F(n):
if n == 0: return 0
elif n == 1: return 1
else: return F(n-1)+F(n-2)

Try it in your favourite language and see that this form requires a lot of time as n gets bigger. In fact, this is O(2n) in time.

Go on on the sites I linked to you and will see this (on wolfram):

Fibonacci Equation

This one is pretty easy to implement and very, very fast to compute, in Python:

from math import sqrt
def F(n):
return ((1+sqrt(5))**n-(1-sqrt(5))**n)/(2**n*sqrt(5))

An other way to do it is following the definition (from wikipedia):

The first number of the sequence is 0,
the second number is 1, and each
subsequent number is equal to the sum
of the previous two numbers of the
sequence itself, yielding the sequence
0, 1, 1, 2, 3, 5, 8, etc.

If your language supports iterators you may do something like:

def F():
a,b = 0,1
while True:
yield a
a, b = b, a + b

Display startNumber to endNumber only from Fib sequence.

Once you know how to generate Fibonacci Numbers you just have to cycle trough the numbers and check if they verify the given conditions.

Suppose now you wrote a f(n) that returns the n-th term of the Fibonacci Sequence (like the one with sqrt(5) )

In most languages you can do something like:

def SubFib(startNumber, endNumber):
n = 0
cur = f(n)
while cur <= endNumber:
if startNumber <= cur:
print cur
n += 1
cur = f(n)

In python I'd use the iterator form and go for:

def SubFib(startNumber, endNumber):
for cur in F():
if cur > endNumber: return
if cur >= startNumber:
yield cur

for i in SubFib(10, 200):
print i

My hint is to learn to read what you need. Project Euler (google for it) will train you to do so :P
Good luck and have fun!

Fibonacci sequence with numbers in 1 line and without using list or array

To calculate the next Fibonacci number simply add together the two previous values. You can do this in a for loop using variables declared outside the loop to hold the previous two values.

Use this statement within your loop

print (value, end = '')

This will print the sequence all on one line with each value separated by a space.

n = int(input()) 

fib0 = 0
fib1 = 1
for x in range(0, n):
nextFib = fib0 + fib1
fib0 = fib1
fib1 = nextFib
print(fib0 , end = ' ')

Fibonacci sequence less than 1000 in R

Instead of checking the value of the last element wrt 1000, for the expected output you should be checking the sum of the last two elements as so.

fib <- c(1,1)
counter <-3
while (fib[counter-2]+fib[counter - 1]<1000){
fib[counter]<- fib[counter-2]+fib[counter-1]
counter = counter+1
}
fib

R function to find Fibonacci numbers

incorporate the following changes

Ms <- function(moving_sum) {
Fib <- numeric(moving_sum + 1) # Use the parameter moving_sum
Fib[1] <- Fib[2] <- 1
for (i in seq(3, moving_sum + 1)) Fib[i] <- Fib[i - 2] + Fib[i - 1]
return(Fib[-1]) # Remove the first number
}

Ms(20)
[1] 1 2 3 5 8 13 21 34 55 89 144 233 377
[14] 610 987 1597 2584 4181 6765 10946


Related Topics



Leave a reply



Submit