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

My two approaches to solve Fibonacci Last Digit Problem on Python

I am a newbie here. If I do something wrongly, please show me tolerance. I tried to solve this problem: Problem_Picture_1 Problem_Picture_2 My first solution didn't work: # Uses python3 import sys # Uses python3 def get_fibonacci_last_digit(n): …
0
votes
1 answer

Javascript closure in fibonacci series using dynamic programming

I am trying to figure out closure in Javascript. Trying to calculate fibonacci series using DP. Here my input will be index: Index = 10 And the output: 55 (As per fibonacci seq: 0,1,1,2,3,5,8,13,21,34,55..) Below version is a working code: function…
winterishere
  • 350
  • 1
  • 5
  • 21
0
votes
1 answer

My function doesn't evaluate some values properly

Background For a coding activity online, I was asked to create a function that took value and returned true if the product of any two consecutive numbers in the Fibonacci Sequence equal that number1. Example An example is 5895, no two numbers…
user13862117
0
votes
0 answers

Understanding the control flow in a code snippet -- Fibonacci Series

var fib = function (n) { if (n == 1) { return [0, 1]; } else { var s = fib(n - 1); s.push(s[s.length - 1] + s[s.length - 2]); return s; } } console.log(fib(5)); In the above code snippet, when I do a…
Stalin
  • 1
0
votes
2 answers

Climb a ladder with n steps, only going by 1, 3 or 5 steps

I have an exercise where I should count the ways of climbing a ladder with n amount of steps, with the following restriction: You can only go up by 1, 3 or 5 steps. I readed that I should use Fibonacci recursion. So I adapted that restriction to the…
Risker
  • 358
  • 3
  • 15
0
votes
3 answers

I'm having problems with Project Euler #2 [Not efficient enough]

This is my code for project Euler #2. The problem is : Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering…
0
votes
1 answer

How are functions called in a Recursive function when there are more than 1 function call in one statement in Python?

def fibonacci(n): if n == 0 or n == 1: # base c a s e s return 1 else: return fibonacci(n-1) + fibonacci(n-2) print(fibonacci(10)) When fibonacci(10) is called , is fibonacci(n-1) resolved first till the base case and…
0
votes
2 answers

Why calculating small fibonacci sequence with Python interpreter faster than PyPy

I was making some fibonacci calculations with PyPy, first i started with bigger numbers, PyPy was a little bit faster, but with small numbers it gives nearly the same performance and some cases normal interpreter is much more faster than pypy is…
Yagiz Degirmenci
  • 16,595
  • 7
  • 65
  • 85
0
votes
1 answer

Adding Counter to recursive Fibonacci program

I'm working with a basic Python MIT free courseware and I have run into a wall with a recursion exercise. The original program takes an integer and provides its Fibonacci using recursion. The book provides the script for the program, but the…
0
votes
0 answers

Converting ThinkScripts CompoundValue Function to python

declare lower; def x = CompoundValue(2, x[1] + x[2], 1); plot FibonacciNumbers = x; This is in thinkscript i want to convert this in python actually i am converting one of Thinkorswim strategy in python backtrader
0
votes
1 answer

Finding Fibonacci bugs keep returning undefined

I am trying to solve the fibonacci in recrusive way and I think i got a bug in my code.I can't figurer out where it is. Example: fib(4) === 3 function fib(n, array = [0, 1], count = 0) { if (n == 0) { return 0; } if (n == 1) { return…
0
votes
0 answers

fibonacci series with inputs

I am pretty confused about the concept of the Fibonacci series. I am trying to make a C++ program of Fibonacci series which asks the user to input 2 different numbers and number of iterations like input one = 9 and input two = 10, the number of…
Loppy2323
  • 82
  • 6
0
votes
2 answers

Program Last Digit of a Large Fibonacci Number for n-th Fibonacci number. C++

I'm trying to solve Fibonacci using C++, but my code shows negative numbers when the output digit limit crosses with big numbers. #include #include using namespace std; int64_t get_fibonacci_last_digit_naive(int n) { …
0
votes
2 answers

is there a preference to coding Fibonacci sequence

Basically my professor coded it another way but mine works too. I just wanted to ask is there any hidden problems in my code that I don't understand? If it's basically the same (which I'm assuming it is), I'll just do it my way. MY CODE int…
Furqan_25
  • 29
  • 6
0
votes
2 answers

Form a fibonacci triangle such that each number is the sum of two numbers above in either left diagonal or the right diagonal

The first few rows are: 1 1 1 2 1 2 3 2 2 3 5 3 4 3 5 8 5 6 6 5 8 13 8 10 9 10 8 13 21 13 16 15 16 13 21 34 21 26 24 25 24 26 21 34 ... I tried in C the following codes: Try 1: #include #include int main(){ int…
user8455790