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

How to correct the error in this fibonacci series program in C?

#include int main() { int sum; int n,num1,num2; printf("Enter a number:"); scanf_s("%d", &n); if (n >= 1) num1 = 1; if (n >= 2) { num2 = 1; sum = num1 + num2; printf("%d\n", sum); …
user12934736
0
votes
2 answers

How to solve a recurrence relation by unrolling?

If I have T(n) = T(n-1) + T(n-2)+ cn; T(1) = T(2) = d How can I apply unrolling to solve a closed-form for T(n)? When I try to unroll this by substitution, my equation gets really long and hard to keep track.
echo Lee
  • 191
  • 1
  • 2
  • 10
0
votes
0 answers

Stuck on Python Fibonacci loop, only returns one value?

def GenerateFibonaci(x): if(x == 0): return 0 elif(x == 1): return 1 else: for i in range(x): return GenerateFibonaci(x-1) + GenerateFibonaci(x-2) print(GenerateFibonaci(x)) I'm attempting to write a program…
ebroker
  • 27
  • 3
0
votes
0 answers

Fibonacci numbers are negative for large inputs

so I have created several Java Fibonacci methods which all have different ways of obtaining fib (ie, recursion, memoization). However, the biggest consistency between each function is that for high input sizes n, i will get a negative Fibonacci…
0
votes
2 answers

Is there a way int is converted to list using python

I have tried to add two numbers in the list. The result, generated was integer. I could like to use append function for list to add this integer to existing list. However, I am getting error the operation is not executable. Fibo is a defined…
Pencil
  • 1
0
votes
2 answers

Even valued terms sum in fibonacci, limit is 4 million

def fib(n): if n<= 1: return n else: return(fib(n-1)+fib(n-2)) def comp(): L=[] for i in range(1,4000000): if i % 2 ==0: L.append(fib(i)) return sum(L) print(comp()) What is wrong with…
cavuscanay
  • 27
  • 4
0
votes
2 answers

Error in finding the right elements from an array in fibonacci sequence c++

So i am changed the code a little bit. It still doesn't work but i think i am little bit closer. The program should check if the number you put in (inside bar[100]) match the fibonacci numbers(stored in fib[30]) and then print the ones that…
0
votes
2 answers

is there a way to sum every nth list like a fibonacci sequance in python and became a new list?

say we have a l list, it made up of 5 or more element which i want to calculate every nth sum in the list just like a fibonacci sequence l=[1,5,6,7,2] finally i would like to have a new list l2 which show the sum of every nth element in the l…
0
votes
1 answer

Can anyone explain to me this code the i++ and y++ areas mostly the else part cause I just can't understand what's happening to get the result

This is the code to do a Fibonacci Generator. I cannot understand what the i++ and y++ are doing and how all this is resulting in giving us the sequence. :( function fibonacciGenerator(n) { var fib = [0, 1]; var i = 0; var y = 1; if (n ===…
0
votes
4 answers

In this Fibonacci function which works, doesn't it pass the number of elements that can be stored in the array?

This is my code and it works. For example, when n=5, it returns 5. Since n=5, and defining arr[0] = 0, isn't there only four more spaces left to store the elements from arr[1] to arr[4]? However, it seems like 6 elements(from arr[0] to arr[5])…
0
votes
0 answers

Python - Iterate over while loop to compile an average runtime of program

So I want to preface this by saying one of the biggest problems with this I'm assuming is the return section of this code. With that being said, exactly what I'm trying to do is based off of my previous question for this code which was answered in…
0
votes
0 answers

Where do +2 values in the vector are coming from?

I am trying to find the last digit of Fibonacci numbers. According to the assignment, I generate the Pisano Period first that is the period with which the sequence of Fibonacci numbers taken modulo n repeats. The cycle of mod 10 equals 60. I wrote…
Daria
  • 163
  • 1
  • 2
  • 8
0
votes
1 answer

Print the first Fibonacci number which has a certain length

So I'm basically just trying to figure out how I can get this program to print out the first Fibonacci number with the same amount of numbers in length as the number that the user has input. The Fibonacci counting portion of this works fine, I'm…
0
votes
3 answers

Recursive function for Fibonacci number

I have to write a simple program as follows: "Given a non-negative integer n, find the nth Fibonacci number using recursion". I think what this means is that, for any value entered by the user, I have to get the Fibonacci number. For example, if the…
0
votes
1 answer

I can't figure out why my fibonacci sequence isn't working

I have to make a little program to calculate a certain iteration of the fibonacci sequence. right now, it always gives out a 2, no matter what i do... public partial class Form1 : Form { int num1=0; int num2=1; int…
Lakkes
  • 25
  • 5