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

2 questions, Python OverflowError: (34, 'Result too large') and wrong function results

1st of all, I'm trying to understand why I'm getting an overflow error. the first function "fibGen" works fine unless I give it an insanely large nth Fibonacci term. #the golden ration function def fibGen(num): for number in…
0
votes
1 answer

Time function seems to always be increasing in my Python Fibonacci program

I'm trying to use a timer to see how fast each function is. My codes work but after running it many times I get slightly different results and wonder if I'm implementing function correctly. I am only going to the 10th Fibonacci number which is 55…
CelestialSky
  • 57
  • 2
  • 14
0
votes
0 answers

Fibonacci Pisano: how to improve (Python3)

I was trying to output F(n) mod m to find the remainder of Fibonacci number, where F(n) could be a very big number: # Uses python3 n, m = [int(x) for x in input().split()] def fib(n): a = [0, 1] if (n <=1): return n else: …
djriles
  • 63
  • 1
  • 4
0
votes
1 answer

How can i calculate the quotients of a Fibonacci series in R

I want to calculate the quotients of a Fibonacci series, such that: I have created a function to calculate the Fbonacci series, and is ok, but i'm having troubles calculating the quotients. This is my function: fibonacci <- function(n){ numbers…
Miguel 2488
  • 1,410
  • 1
  • 20
  • 41
0
votes
1 answer

Implementing fibonacci with array as an input parameter

Task content: The fib() function is to implement a formal formula for the Fibonacci sequence. The input parameter is the number of words returned in the array after finishing the function. The function should check the correctness of the input data.…
user10792998
0
votes
4 answers

How to make a recursive fib-function return the correct value with memoization

I'm learning about memoization in recursive functions and stumbled upon a fibonacci-example on Youtube. I never saw the person run the code so perhaps he wrote it wrong. When I copied the code and tried to run it, i first got an error since I…
0
votes
1 answer

Increment in (a, b = b, a + b) while using a Generator Function

Can someone explain how the increment of a or value of a occurs within the for loop to generate the Fib sequence? I have an understanding of (a, b = b, a + b). However, I am unable to figure how the increment occurs in the for loop when next() is…
RSSregex
  • 179
  • 7
0
votes
1 answer

How to avoid "Infinity" and console.log a large number in Javascript?

I am trying to find the first number in the Fibonacci sequence to contain over 1000 digits. Given a number n (e.g. 4), I found a way to find what place the first number with n-digits has in the Fibonacci sequence as well as a way to find the number…
tommsyeah
  • 121
  • 3
  • 10
0
votes
0 answers

VBA Sum of Fibonacci even numbers till sum < 100

I need your help regarding my homework. The aim is to sum all even numbers from Fibonacci sequence till the sum reach < 100 I have created formula as below Sub fibbo() Dim sum As Long Dim a As Long Dim b As Long sum = 0 Do While sum < 100 …
goodk
  • 1
0
votes
0 answers

MIPS on reading input on fibonacci (iterative)?

I can't figure out what I'm doing exactly, I think I might be missing some stuff. I need to write a Fibonacci sequence by the length of nth times through user input (iterative method). For example if user input is 6: output would be: 5 0, 1, 1, 2,…
Jordles
  • 45
  • 2
  • 9
0
votes
3 answers

fibonacci sequence negative answer after 92 python

I am trying to create a function that gives me the fibonacci sequence for any value of n. However, after the n = 92, I am getting incorrect answers. eg. For n = 93 Expected output = 12200160415121876738 Actual Output = -6246583658587674878 My code…
Alex Ham
  • 151
  • 1
  • 12
0
votes
1 answer

with this Fibonacci sequence question with instruction sets used to make a assembly code

This is a Fibonacci sequence that I recently attempted to turn into a assembly code through the use of instruction set. I am not sure how to go about testing it and was wondering could confirm if I got this right and if not where I went wrong. Also…
0
votes
1 answer

I created an array of ints, user is prompted to pick 2 numbers, I am trying to return the sequence of fibonacci from those 2 numbers

I created an array of ints, user is prompted to pick 2 numbers, I am trying to return the sequence of fibonacci from those 2 numbers #include int main () { int a, b; int nums[48]; for (int i = 0; i < 47; i++) { printf…
P.M.T
  • 19
  • 1
  • 3
0
votes
2 answers

Main function not running? CONFUSED

I am trying to program a module for a class but my main() function does not work I presume the code is right but when I input a number it just goes to the next line, does not stop execution and just allows me to input further numbers, going to the…
CAM
  • 25
  • 9
0
votes
2 answers

Memoization of recursive Fibonacci method not faster?

I've attempted memoization of a recursive Fibonacci method, and it returns the correct number. However, it doesn't appear any faster than before. I'm assuming that this is because I'm not utilizing the array properly to keep track and I'm still…