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

Average Case Complexity for the Fibonacci Series

I am trying to find the average time complexity for the Fibonacci series using mathematical approach. The best case is O(1), while the worst case is O(2^n). The Average case can be found by dividing the sum of times of all cases by the number of…
Alex.Kh
  • 572
  • 7
  • 15
0
votes
0 answers

How to solve population growth algorithm of 3 main variables (based on age)

I was given the assignment to show the population growth of rabbits per month (method), given the 3 variables, one variable for a newborn rabbit, one for a month old rabbit (which can give birth to a pair), and adults. I am uncertain what the syntax…
TITAN696
  • 1
  • 4
0
votes
0 answers

How can I find a large Fibonacci Number?

I have a problem and I have to write a program (in JavaScript) to return the n-th Fibonacci number, this program works just fine when my input is not really big: function nthFibConstantTime(n) { let phi = (1 + Math.sqrt(5)) / 2; return…
Nam V. Do
  • 630
  • 6
  • 27
0
votes
0 answers

How do I make this code output a specific fibonacci term in ARM assembly?

So, in this code I take user input for a specific term in the fibonacci series. And I want to output the number. I made a forloop and attempted to print the result. However the error it gives is "collect2: error: ld returned 1 exit status". Does not…
Valskarx
  • 25
  • 6
0
votes
1 answer

python3: Fibonacci generator not working as expected

I know that the following is a suboptimal implementation of a fibonacci generator compared to this, but I cannot seem to understand why it is not working as expected: def fibonacci_sequence(): fl, fp = 1, 1 while True: yield (fl +…
pkaramol
  • 16,451
  • 43
  • 149
  • 324
0
votes
3 answers

Why do I get a negative output when I run my program to compute the sum of all even Fibonacci numbers?

Background: I'm working on Project Euler problem #2 and I have a class built to solve the problem. For those who haven't done this before, this is the problem: Each new term in the Fibonacci sequence is generated by adding the previous two terms.…
Jodast
  • 1,279
  • 2
  • 18
  • 33
0
votes
1 answer

How to interpret code for counting ways to reach the n’th stair?

I am trying to interpret this code from geeksforgeeks: def fib(n): if n <= 1: return n return fib(n-1) + fib(n-2) def countWays(s): return fib(s + 1) s = 4 print "Number of ways = ", print countWays(s) I can't seem to…
teleluck
  • 35
  • 4
0
votes
0 answers

Fibonacci Sequence in Custom Assembly

I've made a custom 8Bit computer in java similar to Ben Eater's 8Bit breadboard computer. I had Fibonacci working but then I decided to change the ram size and I had to also change the size of the program counter. Ever since that change, the program…
0
votes
2 answers

Fibonacci Sequence printing out all values in an array EDIT

Okay my new question is how I can let the array print out all numbers of the segment. At the moment I can input a number and the code will print out the corresponding value in Fibonacci. However, I would like the array to print out all values…
king
  • 11
  • 6
0
votes
1 answer

find nth fibonacci number where n can vary till 10^9

// i was trying to find (n^n)%1000000007 where n is the nth fibonacci term of the series. example : n=3 ans 4 (2^2) public class Sol_Big { public static void main(String args[]) { int n =10000000; BigInteger a =…
user8076955
0
votes
2 answers

Python function returning Null type data

I'm a trying to calculate the 'nth Fibonacci % m ' for the given values. (Using the pisano's series). Here is the message that the terminal is displaying. error message-> 100 2 Traceback (most recent call last): File "fibag.py", line 27,…
varungupta
  • 155
  • 4
  • 10
0
votes
7 answers

Addition of Even Fibonacci Numbers

I'm trying to solve the 2nd problem on Project Euler where I have to print the sum of all even Fibonacci numbers under 4 million. I'm using the following code but the program is not returning any value. When I replace 4000000 by something small like…
user821
  • 110
  • 10
0
votes
2 answers

Last Digit in Sum of Fibonacci Series (m,n)

I had the doubt in finding the last digit of sum of terms of fibonacci series ranging from index m to index n(Consider starting term to have index 0). I have lots of different ways to solve the problem. But it is required to pass very long cases as…
Kushal Dev
  • 1
  • 1
  • 3
0
votes
2 answers

How do I find the last digit of a very big number that is calculated by a formula?

I have a fibonacci problem where I want to calculate nth fibonacci and want its last digit (digit I get when I'll do it % 10). n would be given and can be up to 10^18. unsigned long long int nthfib(long long int n) { double phi = (1 + sqrt(5))…
0
votes
3 answers

Recursive Fibonacci with integer overflow

I used memoization to reduce the time took to complete recursive fibonacci. But problem is that it cause integer overflow so the numbers are not complted after 50thish number. so how can i prevent integer overflow both in recursive and iterative? i…