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

C Mutex not preventing race condition

This is my first attempt at using a mutex with parallel processing to try to make the results come out correctly. I have a simple program which computes the first 25 Fibonacci numbers in parallel threads. When I have the create and join in the same…
efuddy
  • 105
  • 1
  • 3
  • 11
0
votes
1 answer

Complexity of Fibonacci Fast Recursive Program

def fastfib(n, fib_dict = {0: 1, 1: 1}): if n not in fib_dict: fib_dict[n] = fastfib(n-1, fib_dict) + fastfib(n-2, fib_dict) return fib_dict[n] I think the complexity here is n^2, but I am not sure.
trap28
  • 97
  • 7
0
votes
1 answer

How to print the Fibonacci sequence, skipping every fourth number, replaces the skips with X and starts from 0

/*I was able to output this: 0, 1, 1, X2, 3, 5, 8, X13, 21, 34, 55, X89, 144, 233, 377, X610, 987, 1597, 2584, X4181, 6765, however, I can seem to make it skip the numbers and replaces them with Q. I am stuck on this one. :( import…
zvaniel
  • 25
  • 6
0
votes
1 answer

How would I approach making the Fibonacci Sequence in ARM?

I need to make an ARM assembly program which will print out the Fibonacci Sequence and i'm unsure of how to approach it. The program will ask the user for a number, and when they input that number, the program should print out the Fibonacci sequence…
Entilo
  • 1
  • 1
0
votes
1 answer

How to fix overflow for fibonacci sequence in nasm assembly?

I'm an amateur in assembly and one of my assignments is to do a fibonacci sequence based on my user input. So if my user input is 5, the output will go like this 0 1 1 2 3 It is being placed in an array and then the index…
0
votes
4 answers

Returning Fibonacci series c#

I need to make a method that returns the nth integer in the fibonacci series, the code that I wrote (edited) did not work, could anyone guide me in my for loop section. I need to use a webform and return the fibonacci series to a specific…
TubieNek
  • 23
  • 4
0
votes
5 answers

Finding the nth term of the fibonacci sequence in matlab

I am attempting to write a program that takes a user's input (n) and outputs the nth term of the Fibonacci sequence, without using any of MATLAB's inbuilt functions. I have currently written the following function, however, I wish to alter this code…
compscistudent
  • 107
  • 1
  • 2
  • 11
0
votes
0 answers

Right implementation of two functions related to fibonacci and prime numbers

I have a very long question and some part of the question, involves two functions named gp(a,t) and fibo(a,t). gp(a,t) gets two integer numbers a ,t and must return the smallest prime number that is greater than or equal to (t/100)*a fibo(a,t) gets…
titansarus
  • 136
  • 1
  • 7
0
votes
1 answer

Implementing fibonacci series using greedy approach?

I have implemented fibonacci series using recursion: def fibonacci(n): if n==0: return 0 elif n==1: return 1 else: return fibonacci(n-1) + fibonacci(n-2) I have also implemented it using dynamic…
0
votes
2 answers

Nth Value of Fibonacci Sequence JavaScript (timeout error)

I have a working solution for this Kata (find nth value of Fibonacci Sequence), however I keep getting a timeout error. Can anyone offer advice on how to refactor this to run more efficiently? Thanks in advance! Here is the link with…
Justin Cefai
  • 141
  • 1
  • 3
  • 12
0
votes
2 answers

Strange output values in simple c program

I'm trying to calculate the Fibonacci series in my c program but when i try to output the result, i get 4 weird sequences of numbers which i dont know what they mean.Are they memory addresses or what? What am i doing wrong? #include void…
Stelios Papamichail
  • 955
  • 2
  • 19
  • 57
0
votes
2 answers

Fibonacci shared memory processes between two c files

Hello I have a single file in c that shares memory from the parent to child but I need my code separated into two separate files while still sharing the memory. I need the parent to create the shared memory and get the input of the fib number. Then…
0
votes
3 answers

how does fibonacci with recursion works

int fib(int i) { if(i<2) { return 1; } else { return fib(i-1) + fib(i-2) ; } } I am not able to understand how the statements return fib(i-1) + fib(i-2) is processed??? Does fib(i-1) is process first and fib(i-2) or…
waqas
  • 1
  • 1
0
votes
0 answers

Why this Python code is much slower on Windows?

I've been trying this Python iterative code to get the Fibonacci sequence on Windows and Linux. The problem is that this code takes much longer to execute on Windows and I don't know why. Is it due to the memory management of Windows? Both OS use…
Danubio
  • 93
  • 8
0
votes
4 answers

Java: How to get while loop to only output last line

Just beginning to learn java so this has had me stuck for quite some time now. This is a program to output numbers of the Fibonacci sequence. The goal here is to get the program to output only the last line of the while loop. I've got the program up…
1 2 3
99
100