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

Not sure how to use System.nanoTime() for my program

I need to measure the time in nanoseconds for my project but I'm not sure how to properly implement it into my program. I know I need to use something like: long startTime = System.nanoTime(); ... long endTime =…
0
votes
5 answers

How would I properly make this loop display the first 40 digits of the fibonacci sequence?

As far as I can tell, this looks like it should work. The first two elements are set to 1 (I am ignoring the first 0). The for loop is supposed to loop through the array, and since the fibonacci numbers are equal to the sum of the two preceeding…
Bowlslaw
  • 95
  • 1
  • 1
  • 3
0
votes
1 answer

How to check whether the program goes over the data type storage?

I need to find out how to control the flow of the program so that the program does not crash when the user inserts huge numbers. The goal of the program is to calculate the Fibonacci series, by the user's input. #include using namespace…
0
votes
5 answers

Can't figure out why parameter is changing during recursion in JS fibonacci function

Currently learning JS. Can't figure out at what point in this function "prev1" parameter somehow modified during recursion? If "console.log(prev1)" on each iteration "prev1" is actually modifies, although in none of the code below seems to change…
0
votes
2 answers

Fibonacci Memoization in Python

I wrote this code to calculate the nth Fibonacci number and it works (calculates the right number) but fails because the table doesn't get updated. Anybody know why? # memoization n = 12 table = np.ones(n+1)*-1 def fib3(n): if table[n]==-1: …
DanGoodrick
  • 2,818
  • 6
  • 28
  • 52
0
votes
1 answer

How to fix Code so that Fibonacci Search Algorithm in C# works properly

Code doesn't always find the index. I'm trying to implement the fibonacci search algorithm in C#. Sometimes the algorithm doesn't find the element in the array. I wrote Unit Tests to check Code coverage and saw that 10 % of my code isn't reached. I…
Gino Pensuni
  • 356
  • 4
  • 15
0
votes
1 answer

Resizing dynamic array with the size of Fibonacci numbers

We have a dynamic array with the size of Fibonacci numbers. Assume that F(k) is the array's current size(F(k) is the k-th number of Fibonacci series). We have two rules here: 1)If after inserting an element in the array, the number of array elements…
fem
  • 21
  • 4
0
votes
3 answers

C# problem with Fibonacci sum of even numbers

I'm solving problems from Eurler's page. I'm actually spending a lot of time trying to do something with my code. It's outputting fibonacci numbers correctly, and also summing it(for 15 numbers). But When I put limit to 4 000 000 numbers, then it…
0
votes
3 answers

Fibonacci in a cached solution

I learned a memorized solution to fibonacci in c++ as #include using namespace std; int F[51]; int fib(int n) { if (n<=1) { return n; } if (F[n] != -1) { return F[n]; } F[n] = fib(n-1) +…
AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
0
votes
1 answer

Fibonacci string array revised

Maybe I misunderstand my assignment last time.The actually problem description should be like the following: I have an array: A B AB BAB ABBAB BABABBAB The number of each term of the array is base on the Fibonacci number. Put the n-th string and the…
Neomatrix
  • 31
  • 4
0
votes
1 answer

How to dynamically fill an array with a fibonachi sequence

My question is about dynamic memory allocation and pointer arithmetic.I have a function that is called in the main function and it should fill the array with n longs and populate them with a Fibonacci sequence dynamically. my function is below but…
0
votes
1 answer

1000-Digit Number in Python

I was solving project Euler wherein I came across a question which asked me the index of first 1000 digit fibonacci num. First I used this code but was taking too much of time. def fibonacci(num): if (num==0): return 0; if(num==1): …
Nischaya Sharma
  • 431
  • 2
  • 6
  • 16
0
votes
0 answers

Assembly 16 bit Fibonacci Series

What am I doing wrong? This code only generates accurate Fibonacci series up to 13 after that it's giving the wrong value. Output 1, 1, 2, 3, 5, 8, 13, 29, 37 As its should be 21 after 13 and not the 29 Code org 100h .data num dw 0 .code main…
Hamza AVvan
  • 377
  • 3
  • 10
0
votes
1 answer

Recursive c++ function to print out the elements of a fibonacci tree

So far I have been able to use recursion to print out the fibonacci sequence. For example, the first 6 terms (including the 0'th term) in the sequence is: 6 13 8 5 3 2 1 1 but Im not sure how i'm supposed to use this to print out the entirety of a…
Dov Royal
  • 49
  • 2
  • 9
0
votes
1 answer

A strange error in Fibonacci recursive function in bash: error in the output

I'm trying to write a recursive bash function for Fibonacci sequence. So far I have a code like below: #!/bin/bash Fibo() { case $1 in 0) echo 0;; 1) echo 1;; *) echo $[$[$Fibo $[$1-1]]+$[$Fibo $[$1-2]]] ;; esac } for (( i=0;…
BloodyMary
  • 173
  • 13