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

Rank and unrank fibonacci bitsequence with k ones

For positive integers n and k, let a "k-fibonacci-bitsequence of n" be a bitsequence with k 1 where the 1 on index i describe not Math.pow(2,i) but Fibonacci(i). These positive integers that add up to n, and let the "rank" of a given k-…
0
votes
2 answers

Debugging my recursive Fibonacci Java code and correcting the logic

I'm unable to understand why the output is wrong. I've tried to write a recursive code of printing Fibonacci numbers and got the expected output and a stream of unexpected values. public class FibonacciSeries { static int limitNum = 10; //the…
AzharKhaji
  • 21
  • 1
0
votes
1 answer

Unexpected Result Each Time

I have been trying to implement the Fibonacci series in linear time. I keep getting weird results each time I run the program. I am a newbie in C++. int fib1(int n) { int arr[n]; arr[0] = 0; arr[1] = 1; for (int i = 2; i < n; i++)…
blockByblock
  • 366
  • 2
  • 4
  • 14
0
votes
3 answers

How to write Fibonacci with generator without stop number

I went through Beginning Python fibonacci generator How to write with out stop number where we want to stop. My Code of FIbnocci is below def Fibonnaci(n): if n == 0: return 0 if n == 1: return 1 else: return…
user6882757
0
votes
1 answer

Python function keeps reiterating raw input question in command line

I'm trying to write a Fibonacci sequence that can take a user's input as the multiplier for the generational increase in rabbits, i.e., 4 pairs of offspring produced for each pair of mature rabbits. I have tried storing the integer form of the input…
0
votes
1 answer

Program which calculates the sum of the first 10 Fibonacci numbers that are bigger than 1000

I want to write a program that prints me only the first 10 Fibonacci numbers that > 1000. I tried using head, n=10L but no success. len <- 30 fibvals <- numeric(len) fibvals[1] <- 1 fibvals[2] <- 1 for (i in 3:len) { fibvals[i] <-…
0
votes
1 answer

i want to get the next fibonacci number

I want to find the next fibonacci number in the fibonacci series. Like if i entered 9 then the function should return 13 which is next fibonacci number. i could not understood logic how it can be implemented. Just messing with fibonacci…
0
votes
0 answers

What are the differences in time and space complexity between these 2 algorithms that solve leetcode 873?

I have just solved https://leetcode.com/problems/length-of-longest-fibonacci-subsequence/. The objective is to find the longest Fibonacci sub-sequence in an array of strictly increasing integers. I need some help figuring out the difference in time…
d_darric
  • 387
  • 2
  • 15
0
votes
6 answers

How to print the Fibonacci sequence in Swift Playground using recursion

I am trying to use recursion in Swift to print out the Fibonacci sequence for a number "n" iterations. However, I keep getting the same error. I have already tried doing it without recursion and was able to do it. However, I am now trying to do in a…
0
votes
1 answer

Calculating mod m for large Fibonacci numbers

I am getting two warning (narrowing conversion && control may reach end of non-void function) with the following code. The code compiles however, when I run it it gives this message : Process finished with exit code 139 (interrupted by signal 11:…
Reza Afra
  • 155
  • 1
  • 8
0
votes
4 answers

Fibonacci sequence/number dynamic programming

I'm trying to improve my programming logic skills and I was watching one of the videos on how to approach Fibonacci numbers. After looking at the pseudo code at 6:34 I wrote this: In [14]: def my_fib(x, memo=dict()): ...: if memo.get(x): …
MMT
  • 1,931
  • 3
  • 19
  • 35
0
votes
1 answer

Why does my function push one value too much?

I'm trying to write my own fibonacci function. It should return all fibonacci numbers that are smaller than num in an array. I tried checking currentPush against num with a while loop, but it pushes one value too much to the returned array. Whats…
Nick Frost
  • 37
  • 6
0
votes
2 answers

Index 3 out of bounds for length 3 in fibonacci iterative error after reducing to three element array

I have a task where I should create fibonacci iterative using loop and array. How can I check negative values for fibonacciNumberInOrder? My tutor told me that I don't need allocate N elements in array. I can reduce it to 3element array. And I…
anabanana
  • 49
  • 8
0
votes
1 answer

Unknown Error Thrown in Assembly Fibonacci Program

Yesterday I posted a question about my Recursive Fibonacci program in Assembly. I'm now getting the proper output, thanks to some help from the wonderful folks here, however immediately after the correct output is printed, my program crashes. Here…
muttley91
  • 12,278
  • 33
  • 106
  • 160
0
votes
3 answers

How can I code Fibonacci sequences that involves two inputs, one determines the rule and the other determines the number of numbers produced?

I have a new question regarding a Fibonacci sequence that is kinda unique. It involves two input M and N and looks like this: Example : 2-Fibonacci sequence: {1, 1, 2, 3, 5, 8, 13, ...} 3-Fibonacci sequence: {1, 1, 2, 4, 7, 13, 24, ...} I need to…
codingnoob
  • 39
  • 1
  • 3