Questions tagged [fibonacci]

The Fibonacci sequence is the sequence defined by F(0) = 0, F(1) = 1, F(n + 2) = F(n) + F(n + 1). The first few terms are 0, 1, 1, 2, 3, 5, 8.

The Fibonacci sequence is the sequence defined by

F(0) = 0
F(1) = 1
F(n + 2) = F(n) + F(n + 1).  

The first few terms are 0, 1, 1, 2, 3, 5, and 8.


The most efficient way to compute the first N values is to iterate over an array using the above formula, resulting in O(N) operations (O(N²) if digit or bit operations are counted). The recursive implementation should be generally avoided, since it is O(φN) where φ is the golden ratio and is equal to (1+sqrt(5))/2. However, by using a cache for already computed values, it can be as fast as the iterative implementation.


One efficient method for computing single Fibonacci numbers is

Fib(n) = Round(  Power( 0.5*(1+Sqrt(5)), n ) / Sqrt(5)  );

The square root and power have to be computed in sufficient precision, roughly, Fib(n) requires about 0.2*n decimal digits or about 0.7*n bits in the integer result.


Another method is based on the fact that the matrix

( Fib[n+1] Fib[ n ] )
( Fib[ n ] Fib[n-1] )

is the n-th power of the matrix

( 1 1 ) which is ( Fib[2] Fib[1] )
( 1 0 ) equal to ( Fib[1] Fib[0] )

This is the basis of a halving-and-squaring method that computes Fib[N] in O(log(N)) operations, completely in (big) integer arithmetic.

If one accounts for the complexity of big integer multiplication, the complexity raises to O( M(N) ) digit or bit operations, where M(d) is the complexity of the multiplication of numbers of bit length d. Typical methods have M(d)=O(d*log(d)) for FFT based multiplication, M(d)=O(dw) with w=log2(3) for Karatsuba and smaller w for the Toom-Cook methods.

2345 questions
0
votes
1 answer

How to proof the time complexity of this Fibonacci sequence is O(n)

In the following code, i know that the time complexity is O(n) but how do i proof it in a proper way? Is saying that searching array is O(n) enough? int f[N]; F(n) { if (f[n] >= 0) return f[n]; f[n] = F(n-1) + F(n-2); return f[n]; } int…
Alejandro
  • 11
  • 4
0
votes
1 answer

Why there are negatve numbers in the output?

The task is classical: calculate the number of rabbit pairs after 30 months, taking into account that each mature pare gives three young pair. The code is next: int young = 1; int mature = 0; for(int n=2; n<=31; n++) { int…
0
votes
1 answer

Getting error while defining a sequence of (all) Fibonacci numbers, in SML/NJ

here is my code: val fibs = let val rec fibs_help = fn(n, next) => Cons(n, (fn()=>fibs_help(next, n+next)) ) in fibs_help(0, 1) end; val list = fibs(10) And here is the error: Error: unbound variable or constructor: Cons
0
votes
2 answers

Problem of "for" loop in creating fibonacci series

I was making a program for fibonacci series. x=0 y=1 print (x) print (y) z = None for z in range(1,100,x+y): z=x+y print(z) x = y y = z The problem was that the output showed numbers more than 100. Here is a sample of the…
0
votes
4 answers

Python Iterative Fibonacci

Listen, I know there are a thousand posts about this, and I've spent three hours looking at them all. I know there's a simple reason this code is not working, but I can't figure it out. When it runs, I only want it to return the nth term. I am new…
brinraeven
  • 33
  • 1
  • 4
0
votes
1 answer

Number of time the iterative function is called

Would like to seek a bit of help from StackOverflow. I am trying to print out the sequence of Fibonacci number and also the number of time the iterative function is called which is supposed to be 5 if the input is 5. However, I am only getting…
Headache
  • 27
  • 3
0
votes
4 answers

How do I arrange the output more neatly?

int main() { int n, t1 = 0, t2 = 1,t3 = 2, nextTerm = 0; cout << "Enter the number of terms: "; cin >> n; cout << "Fibonacci number sequence up to " << n << ":" << endl; for (int i = 1; i <= n+1; i++) { if (i == 1) …
ih8coding
  • 27
  • 8
0
votes
2 answers

Fibonacci numbers,find the sum of the even-valued terms whose values do not exceed four million(Project Euler)

Today i work on a problem which is: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the…
0
votes
2 answers

Project Euler Q#2 in "javascript"

the sum of even Fibonacci numbers below 4 mill : i am trying to do it using JavaScript, but i am getting infinity as an answer but if i use small number such as 10, i am getting console.log() output with the result, is it possible to do that in…
G. Santosh
  • 19
  • 7
0
votes
4 answers

Return a specific number from the fibonacci sequence in C

I'm writing a C program that calculates a specific number in the fibonacci sequence, though I'm having trouble returning the sequence as an array.... What am I doing wrong? int fibonacci(int ceiling) { int counter; int num1 = 1, num2 = 1; …
tekknolagi
  • 10,663
  • 24
  • 75
  • 119
0
votes
2 answers

Creating a function that provides the greatest Fibonacci number that is less than or equal to 'n'?

I am trying to create a function that takes an input 'n' and provides an output which is the highest Fibonacci number that's less than or equal to 'n'. For example, if n = 5, then output is 5. If n = 6, then output is 5. My solution is workable but…
0
votes
1 answer

how to use recursion to deal with Fibonacci?

def fib(n): if n == 0 or n == 1: return n else: return fib(n–1)+fib(n–2) I saw the code in the book, but this code has a bug below, SyntaxError: invalid character in identifier Could you please help me about this?
0
votes
2 answers

Return a value given an index as input for the Fibonacci sequence

So given this. //Fibonacci Series using Recursion class fibonacci { static int fib(int n) { if (n <= 1) return n; return fib(n-1) + fib(n-2); } public static void main (String args[]) { int n = 10; …
0
votes
3 answers

Why no answer when trying to sum even Fibonacci numbers

I defined a function to calculate fibonacci numbers which works well. Now I'm trying to add up all the even numbered fibonacci numbers <= n than are also <= 4000000 but I'm getting no output. def fib_iterative(n): a, b = 0, 1 for i in…
newc00der
  • 3
  • 2
0
votes
0 answers

Numbers n such that sigma(F(n)) > 2*F(n) where F(n) is the n-th Fibonacci number

Sorry to be so dumb, but in layman's terms with possible simple examples can someone explain how this works? Numbers n such that sigma(F(n)) > 2*F(n) where F(n) is the n-th Fibonacci number.
Chis
  • 1
1 2 3
99
100