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
6
votes
3 answers

What is a non-mathematical explanation for the Big O of recursive fibonacci?

I read both articles on Big O for Recursive Fibonacci sequence but still do not have a conceptual understanding of why it is O(2^n). This is not a duplicate of this link. Please don't mark as a duplicate. I'm looking for a conceptual answer. …
jennifer
  • 682
  • 5
  • 14
6
votes
4 answers

Why do very large Fibonacci numbers create an ellipse-type shape?

in_range = int(input("range: ")) fibo_list = [] a = 0 b = 1 count = 0 if in_range < 0: print("faulty") elif in_range == 0: print("faulty") else: while count < in_range: c = a + b a = b b = c count += 1 …
Caspian Ahlberg
  • 934
  • 10
  • 19
6
votes
2 answers

My algorithm for calculating the modulo of a very large fibonacci number is too slow

The goal is to compute F(n) modulo m (m up to 10 power 5), where n may be really huge: up to 10 power 18. My algorithm is too slow. My approach: Calculate and store all Fibonacci numbers up to m, then iterate through that array and apply modulo on…
Reinmarius
  • 114
  • 11
6
votes
2 answers

Fibonacci sequence calculator seems correct but can't find similar code online. Is there something wrong?

I made a simple Fibonacci sequence calculator for the first 22 terms: i=1 n=0 while i<=20000: i = i + n n = i - n print(i) Looks like the result is…
6
votes
9 answers

Fibonacci Sequence in VB.net using loop

Please could you help me with displaying the first 10 Fibonacci numbers. My code displays the following result: 1, 2, 3, 5, 8, 13, 21, 34, 55 and I need it to also display the first two Fibonacci numbers (0 and 1). How would I do that? Public…
Wannabe
  • 493
  • 3
  • 10
  • 18
6
votes
1 answer

When I use computeIfAbsent to calculate fibonacci numbers, hashmap size() returns incorrect value

I have the below code: import java.math.BigInteger; import java.util.HashMap; import java.util.Map; public class DynamicFib { private static Map myMap = new HashMap<>(); static { myMap.put(0,…
6
votes
2 answers

How to read user input of integer number and give to function in Forth?

I've written a piece of code in Forth which calculates the 9th Fibonacci number: ." Fibonacci numbers" : INP 9 0 ; : FIB_0 1 0 1 ; : FIB FIB_0 INP DO + SWAP OVER LOOP SWAP . ; Now I want to read the integer number N from user input and give it to…
user3048747
  • 179
  • 1
  • 9
6
votes
2 answers

Scalas (a,b).zipped (or Tuple2.zipped) notion using streams/infinite lists

here is what I thought would be a correct and useful definition of fibonacci nums in scala: lazy val fibs:Stream[Int] = 0 #:: 1 #:: (fibs,fibs.tail).zipped.map(_+_) However, I get the following error: fibs take 10 foreach…
Felix
  • 8,385
  • 10
  • 40
  • 59
6
votes
4 answers

Why doesn't this Fibonacci Number function work in O(log N)?

So the Fibonacci number for log (N) — without matrices. Ni // i-th Fibonacci number = Ni-1 + Ni-2 // by definition = (Ni-2 + Ni-3) + Ni-2 // unwrap Ni-1 = 2*Ni-2 + Ni-3 // reduce the equation = 2*(Ni-3 + Ni-4) + Ni-3 …
Yellowfun
  • 418
  • 1
  • 9
  • 21
6
votes
2 answers

Using Intstream to generate infinite Fibonacci sequence

Im having a little issue figuring out how to use stream to generate an infinite sized, sequential stream which contains all the numbers in a fibonacci sequence. How would I be able to print out an infinite stream? Any advice helps, thanks.
Dflip240
  • 115
  • 1
  • 7
6
votes
5 answers

Print the first n numbers of the fibonacci sequence in one expression

So I've been messing around with Python a bit lately and I'm trying to find a way to output the nth number of the fibonacci sequence in a single expression. This is the code that I've written so far: (lambda f: f if f<2 else (f-1)+(f-2))(n) # n == 1…
nixin72
  • 322
  • 4
  • 16
6
votes
1 answer

Fibonacci mod number c++

I have the following problem: I should compute a fibonacci number mod another given number. I know about the Pisano period and i am trying to implement it here. This is the code: #include #include long long…
StefanL19
  • 1,476
  • 2
  • 14
  • 29
6
votes
2 answers

Find the sum of Fibonacci Series

I have given a Set A I have to find the sum of Fibonacci Sum of All the Subsets of A. Fibonacci(X) - Is the Xth Element of Fibonacci Series For example, for A = {1,2,3}: Fibonacci(1) + Fibonacci(2) + Fibonacci(3) + Fibonacci(1+2) + Fibonacci(2+3) +…
Narendra Modi
  • 851
  • 1
  • 13
  • 29
6
votes
3 answers

Explanation on Fibonacci Recursion

I must understand something about this. It seems like there is no good guide to explain explicitly. What does the function tree look like? static long Fib(int n) { if (n <= 2) { return 1; } return Fib(n - 1) +…
N3wbie
  • 227
  • 2
  • 13
6
votes
2 answers

Unsigned Long Long Won't Go Beyond The 93th Fibonacci Number?

Here's the code I wrote for finding the n-th Fibonacci number: unsigned long long fib(int n) { unsigned long long u = 1, v = 1, t; for(int i=2; i<=n; i++) { t = u + v; u = v; v = t; } return v; } While…
user313335