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
2 answers

Printing user given values in Fibonacci sequence in R

I am trying to make function that prints values of Fibonacci sequence that are under user given value (n). So input 8 will return values (1,1,2,3,5,8) Fib<- function(n){ v=NULL v[1]<-1 v[2]<-1 for(i in 3:n){ v[i]<-v[i-1]+v[i-2] …
JaniH
  • 41
  • 3
0
votes
1 answer

Computing nth Fibonacci number efficiently with Python

I am currently learning Recursion with Micheal.T.Goodrich's "Algorithm and DataStructure with Python". The author suggests a better approach to computing the nth Fibonacci number using recursion without the space complexity of the usual approach.…
0
votes
1 answer

C++ implementation of Binet's matrices for Fibonacci series

For the following code, I am getting this error: no known conversion from 'int' to 'const std::vector, std::allocator > >' for 1st argument. Can someone please point out the…
0
votes
1 answer

Time and space complexity of this Fibonacci solution

I was reading this article on fib solutions (very helpful): https://medium.com/@johanna.fulghum/write-the-fibonacci-sequence-in-every-computational-complexity-9adf5ef12775 But is there a typo on her last solution time and space…
Grshh
  • 31
  • 1
  • 8
0
votes
3 answers

Made a function but it ignores my optional True/False statement

So, I was just making a fibonacci function to return a printed version of the sequence and the variation, but it seems to be ignoring the 'if' statement and jumping to the "else". Don't have a clue of what could be happening here... def fibonacci(r,…
0
votes
2 answers

How to loop a sequence of numbers to push continuously based on a given number? (Fibonacci)

I'm trying to deeply understand how Fibonacci works so I tried a different approach and made my own method but somehow it won't loop and end up pushing only 1 rather than 5 sequences. Here's my code: function myFibo (n){ var myArr = []; …
0
votes
1 answer

Wrote a program that displays the fibonacci number corresponding to user input, but it starts at zero instead of one?

std::cout << "Input a number: "; int userParameter{}; std::cin >> userParameter; int firstNumber{0}; int secondNumber{1}; int sumNumber{firstNumber + secondNumber}; for(int i{1};i <=…
0
votes
0 answers

Finding the sum of n fibonacci numbers

I have to find sum of n fibonacci numbers. what I did is first created a method to find nth fibonacci number. Then what i included in that method is to find the sum recursively. Its like finding factorial of a number recursively but difference is I…
0
votes
1 answer

What should be the correct Fibonacci sequence, where I am doing wrong?

Here is my python code for sequence of 'Fibonacci Numbers '. My code counts the Fibonacci sequence as (0,1,1,2,3........ ). But what I can see at many places Fibonacci sequence as ( 0,1,2,3,5.....). My code generates ' 1 ' as the output for input '…
0
votes
0 answers

Teamscode competition not excepting java code

I am doing a teamscode competition, and for some reason I am getting a Could not find or load main class error. Here is my code: import java.io.*; import java.util.*; public class bunny { public static void main(String[] args) throws…
0
votes
1 answer

Can't understand the behaviour of this algorithm

While studying from the book Algorithms by Michael Goodrich I came upon this "different than usual" recursive algorithm to compute the nth Fibonacci number. The algorithm isn't much explained in the book and the only answer I come up with is (1,1)…
0
votes
2 answers

Fibonacci Problem #2 from Project Euler with PHP

The question goes like this: 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…
nubicurio
  • 165
  • 1
  • 2
  • 15
0
votes
2 answers

Javascript recursive function not return the result

this maybe is not the biggest challenging problem but I've got curious about it. I did a simple code just to get the Fibonacci value based on the user input without recursion and works just fine. Very simple code: function fib(n) { let…
HenriqueAdriano
  • 119
  • 1
  • 7
0
votes
2 answers

Why Fibonacci numbers formula while loop return undefined at the last number?

Why the last number of my Fibonacci number return undefined? function sumFibs(num) { var a = 1, b = 0, temp; while (num >= 0){ temp = a; a = a + b; b = temp; num--; console.log(b); }; } console.log(sumFibs(4));//1,1,2,3,5,undefined
Consxious
  • 53
  • 6
0
votes
0 answers

fibonacci sequence function with lru_cache doesn't show all fibonacci numbers

I have this function that can print large lists of numbers from fibonacci sequence. but it is pretty slow due to recursion. so I found out about the lru_cache. but it doesn't start the sequence from 1. In this example, from 1 to 501, it starts…
John Popa
  • 23
  • 4