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
4
votes
1 answer

Fibonacci Sequence using loop and recur

I am doing the Project Euler challenge in Clojure and I want to find the sum of all the even numbers in a fibonacci sequence up to a certain number. The code for a function that does this is below. I know there are quicker and easier ways of doing…
adamjmarkham
  • 2,150
  • 2
  • 18
  • 26
4
votes
5 answers

Finding the 1500000th Fib Number Using C++

I wrote the following code to find the 1500000th Fibonacci number(Please ignore horrible indenting, I wrote this in about 2 minutes). I need it as a string. This should, hypothetically work: #include #include #include…
TimeCoder
  • 1,379
  • 3
  • 12
  • 13
4
votes
1 answer

Can we use a pandas data frame to calculate the next value using a previous value? A good example would be the Fibonacci numbers

So I understand we can use pandas data frame to do vector operations on cells like df = pd.Dataframe([a, b, c]) df*3 would equal something like : 0 a*3 1 b*3 2 c*3 but could we use a pandas dataframe to say calculate the Fibonacci sequence ? I…
manny
  • 317
  • 1
  • 2
  • 9
4
votes
1 answer

CSS grid like fibonacci

this is the desired result (similar to fibonacci's grid): I know this is posible using css grid, but as i'm not as familiar as I would like to, I tried using this https://cssgrid-generator.netlify.com/ like so: .parent { display: grid; …
Toni Michel Caubet
  • 19,333
  • 56
  • 202
  • 378
4
votes
5 answers

How can I generate the Fibonacci sequence using Clojure?

(ns src.helloworld) (defn fibonacci[a b] (println a b (fibonacci (+ b 1) a + b))) (fibonacci 0 1) I'm new to Functional Programming and decided to start learning Clojure as it's very different from C#. I'd like to broaden my horizons. Here's the…
Only Bolivian Here
  • 35,719
  • 63
  • 161
  • 257
4
votes
8 answers

Fibonacci sequence backward

Here is the code: class Fibonacci { static final int MIN_INDEX = 1; public static void main (String[] args){ int high = 1; int low = 1; String jel; System.out.println("9: " + high); for (int i = 8; i >=…
Zwiebel
  • 1,605
  • 4
  • 21
  • 37
4
votes
1 answer

Recursion, memoization and mutable default arguments in Python

"Base" meaning without just using lru_cache. All of these are "fast enough" -- I'm not looking for the fastest algorithm -- but the timings surprised me so I was hoping I could learn something about how Python "works". Simple loop (/tail…
user11172168
4
votes
4 answers

Why this implementation of Fibonacci is extremely fast?

This implementation of Fibonacci is easy to understand but very slow: fib 0 = 0 fib 1 = 1 fib n = fib (n-1) + fib (n-2) Following implementation of Fibonacci is hard to understand but super fast. It calculates 100,000th Fibonacci number instantly…
user6435535
4
votes
4 answers

Last Digit of a Large Fibonacci Number fast algorithm

I'm trying to solve Fibonacci using java, but my code takes so long with big numbers. Problem Description Task. Given an integer , find the last digit of the th Fibonacci number (that is, mod 10). Input Format. The input consists of a single…
Kero Fawzy
  • 690
  • 1
  • 7
  • 19
4
votes
1 answer

Implementing Fibonacci sequence using pure lambda calculus and Church numerals in Racket

I've been struggling with the Lambda Calculus for quite some time now. There are plenty of resources that explain how to reduce nested lambda expressions, but less so that guide me in writing my own lambda expressions. I'm trying to write the a…
jsifferman
  • 51
  • 1
  • 5
4
votes
1 answer

Why is using a third variable faster than an addition trick?

When computing fibonacci numbers, a common method is mapping the pair of numbers (a, b) to (b, a + b) multiple times. This can usually be done by defining a third variable c and doing a swap. However, I realised you could do the following, avoiding…
AJF
  • 11,767
  • 2
  • 37
  • 64
4
votes
2 answers

Fibonacci sequence primes

I wasnt able to find any topic on this despite multiple searches. Apologies if this was already covered here. Can anyone point me in the right direction for further research on the below topic: I have played recently with the Fibonacci sequence and…
4
votes
2 answers

How can two similar functions have different polymorphic types in Haskell?

Im pretty much new to Haskell, so if Im missing key concept, please point it out. Lets say we have these two functions: fact n | n == 0 = 1 | n > 0 = n * (fact (n - 1)) The polymorphic type for fact is (Eq t, Num t) => t -> t Because n…
James
  • 267
  • 3
  • 5
  • 14
4
votes
4 answers

Variable assignment query in python

I am writing Fibonacci code in python. The below solution is mine. While the other below solution is from python.org. Can anyone tell me why it yields a different answer even though the logic of assigning the variables is the same?
4
votes
2 answers

"Three-bonacchi" sequence

We have a sequence with first 3 elements: t_1 = t_2 = t_3 = 1 The rest of the sequence is defined by rule: t_n = t_(n-1) + t_(n-2) + t_(n-3) (like Fibonacci sequence but for 3 numbers). t = {1; 1; 1; 3; 5; 9; 17; 31; ...} The task is to find the…
Legonaftik
  • 1,350
  • 3
  • 17
  • 33