Algorithm learning #02--Fibonacci Fibonacci sequence algorithm optimization

Source: Internet
Author: User
Tags pow

Algorithm List

In this paper, the optimal algorithm is found from the perspective of time efficiency and occupied space memory.

    1. Classic recursive algorithm recursive algorithm (very slow)
    2. Dynamic storage algorithm programming (slow)
    3. Matrix Power algorithm exponentiation (FAST)
    4. Multiple formula algorithm fast doubling (soon)
    5. Multiple formula algorithm + fast multiplication fast doubling with Karatsuba (fastest)
Fibonacci Series

1. Introduction to the series

The Fibonacci sequence (Fibonacci sequence), also known as the Golden Section series, was introduced by the mathematician Leonardo's Fibonacci (Leonardoda Fibonacci) as an example of rabbit reproduction, hence the "rabbit sequence", referring to such a sequence:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, ...

2. Sequence rules

When we draw these numbers into squares, we get a spiral-enlarged graph, as follows.

The law of the sequence is: f (n) = f (n-1) + f (n-2)

And when n tends to infinity, the ratio of the previous item to the latter becomes more and more close to the Golden Section 0.618(or the fractional fraction of the latter to the previous one is getting closer to 0.618).

1÷1=1,1÷2=0.5,2÷3=0.666...,3÷5=0.6,5÷8=0.625 ...,
55÷89=0.617977 ... 144÷233=0.618025 ... 46368÷75025=0.6180339886 ...

The closer to the back, the more close to the gold ratio.

3. General formula:

or (Φ is 1.618034 ... )

Classical recursive algorithm

1. Recursive formulas

F (n) = f (n-1) + f (n-2)

2. Code

Private Static Longi =0; Public Static Long F(intN) {if(n = =0)return 0;if(n = =1)return 1; i++;//Record calculation times    returnF (n1) + F (n2);} Public Static void Main(string[] args) { for(intn =0; N < +; n++) System. out. println (n +" "+ F (n) +" "+ i +" "+ (Long) Math.pow (1.618, n+1));}

Output:
0 0 0 1
1 1 0 2
2 1 1 4
3 2 3 6
4 3 7 11
5 5 14 17
.......
35 9227465 39088132 33360044
36 14930352 63245948 53976552
37 24157817 102334116 87334061
38 39088169 165580101 141306511
39 63245986 267914255 228633935

3. Assessment

Time efficiency –> number of Operations O (φ^n) . The larger the N, the closer this value is, see the last two columns.
Space memory –> consumes memory O (n).

Dynamic storage Algorithms

The calculated F (n-1) and F (n-2) are stored without recalculation.

1. Code

 Public Static Long F(intN) {LongA =0, B =1, C, I;if(n = =0)returnA for(i =2; I <= N;        i++) {c = a + B;        A = b;    b = C; }returnb;} Public Static void Main(string[] args) { for(intn =0; N < -; n++) System. out. println (n +" "+ F (n));}

2. Assessment
Time efficiency –> The number of Operations O (n) .
Space memory –> consumes memory O (1).

Matrix Power algorithm

1. Formula

2. Proof

3. Code

public static long fib (int n) {long[][] f = {{1,1},{1,0}};if (n = = 0)return 0;if (n = = 1)return 1;return power (f, n-1);}public Static long Power (long[][] F, int n) {if (n = = 0)return 0;if (n = = 1)return 1;long[][] m = {{1,1},{1,0}};power (F, N/2);long x = multiply (f, f);if (n% 2! = 0)x = Multiply (f, m);return x;} public static long Multiply (long[][] F, long[][] m) {long x = f[0][0]*m[0][0] + f[0][1]*m[1][0]; Long y = f[0][0]*m[0][1] + f[0][1]*m[1][1]; Long z = f[1][0]*m[0][0] + f[1][1]*m[1][0]; Long w = f[1][0]*m[0][1] + f[1][1]*m[1][1]; f[0][0] = x; f[0][1] = y; f[1][0] = Z; f[1][1] = W; return x;} public static void Main (string[] args) {for (int n = 0; n <; n++)System.out.println (n + "" + FIB (n));}

4. Assessment
Time efficiency –> number of Operations O (LOGN) .
Space memory –> consumes memory O (1).

Multiple formula algorithm

1. Formula

2. Proof

On the basis of the Matrix power formula.

3. Code

Put it in the next algorithm.

Multiple formula algorithm + fast multiplication

1. Formula

Or the same as the multiple formula algorithm, but the multiplication is replaced by fast multiplication.

The fast multiplication is introduced first.

Karatsuba multiplication is a fast multiplication algorithm. Presented by Anatolii Alexeevitch Karatsuba in 1960 and published in 1962. In general, we do high-precision multiplication, the complexity of the common algorithm is O (n^2), and the Karatsuba algorithm is O (3n^log3) ≈o (3n^1.585).

2. Proof

The Karatsuba algorithm is mainly used to multiply two large numbers, the principle is to divide the large number into two segments into smaller digits, and then do 3 times multiplication. The derivation process is as follows:

Computes two large number of XY multiplies, taking a positive integer m.

3. Code

//multiplier formula algorithmPrivate Static Long Fibonacci(intN) {LongA =0;Longb =1; for(inti = to; I >=0; i--) {//n Max is 2^32-1, so it only needs to be shifted 32 times.        LongD = A * (b *2-a);LongE = A * a + b * b;        A = D; b = e;if(((n >> i) &1) !=0) {Longc = a + B;            A = b;        b = C; }    }returnA;}//Multiplier formula algorithm + fast algorithmPrivate Static Long Fibonacciwithkaratsuba(intN) {LongA =0;Longb =1; for(inti = to; I >=0; i--) {LongD = Karatsuba (A, b *2-a);LongE = Karatsuba (A, a) + Karatsuba (b, b);        A = D; b = e;if(((n >> i) &1) !=0) {Longc = a + B;            A = b;        b = C; }    }returnA;}//Fast algorithm Public Static Long Karatsuba(LongXLongY) {if((X <Ten) || (Y <Ten)){returnx * y;    } String S1 = string.valueof (x); String s2 = string.valueof (y);intMaxLength = Math.max (S1.length (), s2.length ());intm = (int) Math.pow (Ten, maxlength/2);//Take 10 (maxlength length half) power to divisor    LongXhigh = x/m;LongXLow = x% m;LongYhigh = y/m;LongYlow = y% m;LongA = Karatsuba (Xhigh, Yhigh);Longb = Karatsuba ((XLow + Xhigh), (Ylow + Yhigh));Longc = Karatsuba (XLow, Ylow);returnA * M * m + (B-A-c) * M + C;} Public Static void Main(string[] args) { for(intN =0; N < -; n++) System. out. println (N +" "+ Fibonacci (N) +" "+ Fibonacciwithkaratsuba (N));}

The output is the same.

0 0 0
1 1 1
2 1 1
3 2 2
4 3 3
5 5 5
....
45 1134903170 1134903170
46 1836311903 1836311903
47 2971215073 2971215073
48 4807526976 4807526976
49 7778742049 7778742049
50 12586269025 12586269025

4. Assessment
Time efficiency –> number of Operations O (LOGN) .
Space memory –> consumes memory O (1).

Algorithm time Efficiency comparison

Operating Environment:
Intel Core 2 Quad Q6600 (2.40 GHz)
Single Thread
Windows XP SP 3, Java 1.6.0_22.

Unit NS.

Resources:
Https://www.nayuki.io/page/fast-fibonacci-algorithms

Https://en.wikipedia.org/wiki/Karatsuba_algorithm

Https://www.mathsisfun.com/numbers/fibonacci-sequence.html

Algorithm learning #02--Fibonacci Fibonacci sequence algorithm optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.