3 Computational methods for the "Java" Fibonacci sequence (Fibonacci Sequence, rabbit Sequence) (recursive implementation, recursive value cache implementation, loop implementation)

Source: Internet
Author: User

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13 .....

His rule is that the first item is 0, the second item is 1, and the third item starts with the sum of the first two.

> Recursive implementation

See this rule, the first thought of course is recursive algorithm to achieve, so wrote the following paragraph:

 Public classRecursionforfibonaccisequence { Public Static voidMain (string[] args) {System.out.println (recursion (10)); }         Public Static DoubleRecursion (Doublei) {if(i = = 0) {Printresult (I,0); return0; }        if(i = = 1) {Printresult (I,1); return1; }                Doubleresult = Recursion (i-1) + recursion (i-2);                Printresult (i, result); returnresult; }        /*** Print Results*/     Public Static voidPrintresult (DoubleIDoubleresult) {System.out.println (i+ "+" +result); }    }

It works correctly, such as the result of the 10th item being calculated as 55.

However, calculating large numbers of data is slow and slow, because there are too many repeated computations.

Log:

1.03.0, 2.01.0-1.00.0, 1.01.0, 0.02.0, 0.02.0, 1.0, 1.00.0, 1.04.0- > 1.00.0, 0.02.0, 1.01.0, 1.03.0, 2.05.0, 5.01.0--1.00.0 1.04.0, 3.06.0-8.01.0, 1.00.0, 0.02.0, 1.00.0, 1.03.0, 2.01.0, 0.02.0 1.04.0 3.01.0, 1.00.0, 0.02.0, 1.00.0, 0.02.0, 1.01.0, 1.03.0, 2.01.0, 0, T 1.00.0, 0.02.0-1.01.0, 5.07.0, 13.01.0, 1.03.0, 1.03.0, 2.05.0, 2.01.0-1.00 .0, 0.02.0, 1.04.0, 3.01.0, 1.00.0, 0.02.0, 1.01.0, 1.03.0-2.05.0 5.01.0 Gt 2.01.0, 1.00.0-0.02.0, 1.01.0, 1.03.0, 1.04.0, 1.00.0, 0.02.0, 3.06.0 1.03.0 2.01.0, 1.00.0, 0.02.0, 0.02.0, 1.01.0, 1.04.0, 21.01.0, 1.00.0, 0, Gt 3.01.02.05.0, 5.01.0-1.00.0, 1.01.0, 1.03.0, 0.02.0, 1.00.0, 0.02.0, 1.01.0 3.06.0 8.01.0, 1.00.0, 0.02.0, 0.02.0, 1.04.0, 1.01.0, 2.01.0, 1.00.0, 0, T 1.04.0, 3.01.0-1.00.0, 1.00.0, 0.02.0, 0.02.0, 1.03.0, 2.01.0, 1.01.0 1.00.0, 0.02.0, 1.01.0, 13.09.0, 34.01.0, 1.03.0, 2.05.0, 5.07.0, 0- > 1.00.0, 0.02.0, 1.04.0, 3.01.0, 1.00.0, 0.02.0--1.01.0 1.03.0, 2.01.0-1.00.0, 0.02.0, 1.01.0, 0.02.0, 5.01.0, 1.00.0, 1.04.0 1.03.0 2.01.0, 1.00.0, 0.02.0, 0.02.0, 1.01.0, 1.04.0, 8.01.0, 1.00.0, 0, T 1.03.0, 2.05.0-5.07.0, 0.02.0, 1.01.0, 13.01.0, 3.01.0, 1.00.0, 1.00.0-0.02 .0-1.01.0-&Gt 1.04.0, 3.01.0-1.00.0, 1.00.0, 0.02.0, 0.02.0, 1.03.0, 2.01.0, 1.01.0 1.01.0 1.03.0, 2.01.0, 1.00.0, 1.00.0, 0.02.0, 0.02.0, 2.05.0, 5.01.0, 0, T 1.04.0, 3.06.0, 8.08.0, 21.010.0, 55.055.0
View Code

> Recursive value Cache implementation

With the most intuitive way to optimize, since there are too many repetitions, and the results of repeated calculations are the same, then we cache the result set of the repeating calculation.

Because the recursive efficiency of the above example is low, can not execute too many items, so only to 10, and the following writing efficiency greatly improved, so we perform to 100 look.

ImportJava.util.HashMap;ImportJava.util.Map; Public classCacheforfibonaccisequence { Public Static voidMain (string[] args) {System.out.println (recursion (100)); }        //cache computed result set     Public Staticmap<double, double> map =NewHashmap<double, double>();  Public Static DoubleRecursion (Doublei) {if(i = = 0) {Printresult (I,0); return0; }        if(i = = 1) {Printresult (I,1); return1; }                if(Map.containskey (i)) {returnMap.get (i); }                Doubleresult = Recursion (i-1) + recursion (i-2);        Printresult (i, result);                Map.put (i, result); returnresult; }        /*** Print Results*/     Public Static voidPrintresult (DoubleIDoubleresult) {System.out.println (i+ "+" +result); }    }

Log:

1.03.0, 2.04.0-3.05.0, 1.01.0, 5.06.0, 1.0, 0.02.0, 1.00.0,  21.09.0, 34.010.0, 55.011.0, 89.012.0, 144.013.0, 233.014.0, 377.015.0 6765.021.0-10946.022.0, 2584.019.0, 4181.020.0, 1597.018.0, 987.017.0- > 28657.024.0, 75025.026.0, 121393.027.0, 196418.028.0, 46368.025.0, 514229 9227465.03, 3524578.034.0, 5702887.035.0, 2178309.033.0, 1346269.032.0, 832040.031.0, 030.0, 1, 6.3245986e740.0, 1.02334155e841.0, 3.9088169e739.0, 2.4157817e738.0, 1.4930352e737.0, 6.0, .7.01408733e845.0, 1.13490317e946.0, 4.33494437e844.0, 65580141e842.0, 2.67914296e843.0, 1.836311903e947.0, 2.971215073e948.0, 4.807526976e949.0, 7.778742049e950.0, 1.2586269025e1051.0- > 2.0365011074e1052.0-3.29511.39583862445e1156.0, 8.6267571272e1055.0, 280099e1053.0, 5.3316291173e1054.0, 9.56722026041e1160.0, 5.91286729879e1159.0, 2.25851433717e1157.0, 3.65435296162e1158.0, 6.557470319842e1264.0, 4.052739537881e1263.0, 1.54800875592e1261.0, 2.504730781961e1262.0, 4.4945570212853e1368.0, 2.7777890035288e1367.0, 1.0610209857723e1365.0, 1.7167680177565e1366.0, 7.2723460248141e1369.0, 1.17669030460994e1470.0, 1.90392490709135e1471.0, 3.08061521170129e1472.0- > 4.98454011879264e1473.0, 8.06515533049393e1474.0, 1.304969544928657e1575.0 8.944394323791464e1579.0, 5.527939700884757e1578.0, 2.11148507797805e1576.0, 3.416454622906707e1577.0, 1.447233402467622e1680.0, 2.3416728348467684e1681.0, 3.7889062373143904e1682.0, 6.1305790721611584e1683.0, 9.9194853094755488e1684.0, 1.60500643816367072e1785.0, 2.5969549691112256e1786.0-4.2019614072748966e1787.0, 6.7989163763861222e1788.0, 1.10008777836610189e1889.0, 1.77997941600471398e1890.0- > 2.880067194370816e1891.0, 4.6600466103755305e1892.0, 7.5401138047463465e1893.0 1.2200160415121877e1994.0, 1.9740274219868226e1995.0, 3.19404346349901e1996.0, 5.168070885485833e1997.0, 8.362114348984843e1998.0, 1.3530185234470676e2099.0, 2.189229958345552e20100.0-3.54224848179262e203.54224848179262e20
View Code

> Cycle mode

And we can also use the Loop way:

 Public classForeachforfibonaccisequence { Public Static voidMain (string[] args) {System.out.println (foreach (100)); }         Public Static DoubleforeachDoublei) {if(I <=0d) {return0d; }                if(i = =1d) {return1d; }                DoubleTemp1 =0d; DoubleTemp2 =1d; DoubleTempSum = 0;  for(DoubleD = 2; D <= i; d++) {TempSum= Temp1 +Temp2;                        Printresult (d, TempSum); Temp1=Temp2; Temp2=TempSum; }                returnTempSum; }        /*** Print Results*/     Public Static voidPrintresult (DoubleIDoubleresult) {System.out.println (i+ "+" +result); }    }

Log:

5.06.0, 8.07.0-13.08.0, 2.04.0, 3.05.0, 21.09.0, 2.0, 1.03.0, 34.010.0-55.0  377.015.0, 610.016.0, 987.017.0, 1597.018.0, 144.013.0, 233.014.0, 89.012.0, 11.0, 17711.023.0 28657.024.0, 46368.025.0, 10946.022.0, 6765.021.0, 4181.020.0, 2584.019.0, ; 1346269.0, 514229.030.0, 832040.031.0, 317811.029.0, 196418.028.0, 121393.027.0, 75025.026.0, 2.4157817, 9227465.036.0, 1.4930352e737.0, 5702887.035.0, 3524578.034.0, 2178309.033.0, 32.0, E738.0, 3.9088169e739.0, 6.3245986e740.0, 1.02334155e841.0, 1.65580141e842.0- > 4.33494437e844.0, 7.01408733e845.0, 1.13490317e946.0, 1.836311903e947.0, 2.971215073e948.0- > 4.807526976e949.0, 7.778742049e950.0, 1.2586269025e1051.0, 2.0365011074e1052.0, 3.2951280099e1053.0-5.3316291173e1054.0 3.65435296162e1158.0, 2.25851433717e1157.0, 8.6267571272e1055.0, 1.39583862445e1156.0, 2.504730781961e1262.0, 1.54800875592e1261.0, 5.91286729879e1159.0, 9.56722026041e1160.0, 1.7167680177565e1366.0, 1.0610209857723e1365.0, 4.052739537881e1263.0, 6.557470319842e1264.0, 1.17669030460994e1470.0, 7.2723460248141e1369.0, 2.7777890035288e1367.0, 4.4945570212853e1368.0, 1.90392490709135e1471.0, 3.08061521170129e1472.0, 4.98454011879264e1473.0, 8.06515533049393e1474.0- > 1.304969544928657e1575.0, 2.11148507797805e1576.0, 3.416454622906707e1577.0 5.527939700884757e1578.0, 8.944394323791464e1579.0, 1.447233402467622e1680.0, 2.3416728348467684e1681.0, 3.7889062373143904e1682.0, 6.1305790721611584e1683.0, 9.9194853094755488e1684.0, 1.60500643816367072e1785.0, 2.5969549691112256e1786.0, 4.2019614072748966e1787.0-6.798916376386122.880067194370816e1891.0, 1.77997941600471398e1890.0, 22e1788.0, 1.10008777836610189e1889.0, 4.6600466103755305e1892.0, 7.5401138047463465e1893.0, 1.2200160415121877e1994.0, 1.9740274219868226e1995.0, 3.19404346349901e1996.0, 5.168070885485833e1997.0, 8.362114348984843e1998.0, 1.3530185234470676e2099.0, 2.189229958345552e20100.0, 3.54224848179262e203.54224848179262e20
View Code

3 Computational methods for the "Java" Fibonacci sequence (Fibonacci Sequence, rabbit Sequence) (recursive implementation, recursive value cache implementation, loop implementation)

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.