Python's Recursive Algorithm Learning (2): Concrete implementation: Fibonacci and the traps therein

Source: Internet
Author: User

1. Fibonacci

What is Fibonacci, the Fibonacci amount is a sequence of integers sorted by the definition as follows;

Fn = Fn-1 + Fn-2
F0 = 0 and F1 = 1with

That is, 0,1,1,2,3,5,8,13 .....

Recursive implementations:

def fib (n):     if n = = 0        :return  0    elif n = = 1        :return 1    else:        return fib (n-1) + fib (n-2)

Non-recursive implementations:

def Fibi (n):     = 0, 1 for in      Range (n):        = B, A + b    return A

Here, if we carefully debug, we will find that the recursive implementation, will consume more time, here the test is as follows:

 fromTimeitImportTimer fromFiboImportfibt1= Timer ("fib (Ten)","From Fibo import fib") forIinchRange (1,41): S="fib ("+ STR (i) +")"T1= Timer (S,"From Fibo import fib") time1= T1.timeit (3) s="Fibi ("+ STR (i) +")"T2= Timer (S,"From Fibo import Fibi") time2= T2.timeit (3)    Print("n=%2d, fib:%8.6f, Fibi:%7.6f, Percent:%10.2f"% (i, time1, time2, time1/time2))

The results are as follows;

C:\Python35\python.exe C:\pylearn\bottlelearn\fibnaqie.pyn= 1, fib:0.000002, fibi:0.000003, percent:0.55N= 2, fib:0.000002, fibi:0.000003, percent:0.73N= 3, fib:0.000003, fibi:0.000003, percent:1.20N= 4, fib:0.000005, fibi:0.000003, percent:1.80N= 5, fib:0.000007, fibi:0.000003, percent:2.45N= 6, fib:0.000012, fibi:0.000004, percent:3.31N= 7, fib:0.000022, fibi:0.000003, percent:6.50N= 8, fib:0.000030, fibi:0.000004, percent:8.38N= 9, fib:0.000049, fibi:0.000003, percent:14.58N=10, fib:0.000078, fibi:0.000004, percent:20.07N=11, fib:0.000126, fibi:0.000004, percent:35.00N=12, fib:0.000203, fibi:0.000004, percent:52.29N=13, fib:0.000330, fibi:0.000004, percent:79.20N=14, fib:0.000537, fibi:0.000004, percent:120.94N=15, fib:0.000925, fibi:0.000005, percent:196.18N=16, fib:0.001693, fibi:0.000007, percent:244.16N=17, fib:0.007242, fibi:0.000011, percent:652.70N=18, fib:0.004422, fibi:0.000007, percent:637.64N=19, fib:0.010322, fibi:0.000008, percent:1240.40N=20, fib:0.010813, fibi:0.000007, percent:1443.78N=21, fib:0.025547, fibi:0.000011, percent:2246.24N=22, fib:0.035421, fibi:0.000011, percent:3192.22N=23, fib:0.060877, fibi:0.000008, percent:7837.86N=24, fib:0.090561, fibi:0.000007, percent:12091.41N=25, fib:0.132058, fibi:0.000008, percent:16415.97N=26, fib:0.227813, fibi:0.000008, percent:29330.54N=27, fib:0.557644, fibi:0.000014, percent:38659.17N=28, fib:0.578976, fibi:0.000009, percent:65224.25N=29, fib:1.133326, fibi:0.000008, percent:140882.03N=30, fib:1.454107, fibi:0.000009, percent:169095.94N=31, fib:2.274395, fibi:0.000009, percent:264486.10N=32, fib:3.817956, fibi:0.000009, percent:430110.09N=33, fib:5.923710, fibi:0.000009, percent:688859.58N=34, fib:9.629423, fibi:0.000009, percent:1020986.44N=35, fib:15.910085, fibi:0.000009, percent:1686911.18N=36, fib:26.556680, fibi:0.000009, percent:2901071.88N=37, fib:43.014073, fibi:0.000016, percent:2673506.31

Why is that? Let us consider the calculation of the recursive process, as follows;

As can be seen from the tree, there are repeated calculations, (f (1) calculated many times.

Here, try to make an improvement, introduce a temporary dictionary, and make a save of the computed content:

Memo = {0:0, 1:1}def  FIBM (n):    if not in memo:         = FIBM (n-1) + FIBM (n-2)    return Memo[n]

To re-test the time:

 fromTimeitImportTimer fromFiboImportfibt1= Timer ("fib (Ten)","From Fibo import fib") forIinchRange (1,41): S="FIBM ("+ STR (i) +")"T1= Timer (S,"From Fibo import FIBM") time1= T1.timeit (3) s="Fibi ("+ STR (i) +")"T2= Timer (S,"From Fibo import Fibi") time2= T2.timeit (3)    Print("n=%2d, fib:%8.6f, Fibi:%7.6f, Percent:%10.2f"% (i, time1, time2, time1/time2))
N= 1, fib:0.000011, fibi:0.000015, percent:0.73N= 2, fib:0.000011, fibi:0.000013, percent:0.85N= 3, fib:0.000012, fibi:0.000014, percent:0.86N= 4, fib:0.000012, fibi:0.000015, percent:0.79N= 5, fib:0.000012, fibi:0.000016, percent:0.75N= 6, fib:0.000011, fibi:0.000017, percent:0.65N= 7, fib:0.000012, fibi:0.000017, percent:0.72N= 8, fib:0.000011, fibi:0.000018, percent:0.61N= 9, fib:0.000011, fibi:0.000018, percent:0.61N=10, fib:0.000010, fibi:0.000020, percent:0.50N=11, fib:0.000011, fibi:0.000020, percent:0.55N=12, fib:0.000004, fibi:0.000007, percent:0.59N=13, fib:0.000004, fibi:0.000007, percent:0.57N=14, fib:0.000004, fibi:0.000008, percent:0.52N=15, fib:0.000004, fibi:0.000008, percent:0.50N=16, fib:0.000003, fibi:0.000008, percent:0.39N=17, fib:0.000004, fibi:0.000009, percent:0.45N=18, fib:0.000004, fibi:0.000009, percent:0.45N=19, fib:0.000004, fibi:0.000009, percent:0.45N=20, fib:0.000003, fibi:0.000010, percent:0.29N=21, fib:0.000004, fibi:0.000009, percent:0.45N=22, fib:0.000004, fibi:0.000010, percent:0.40N=23, fib:0.000004, fibi:0.000010, percent:0.40N=24, fib:0.000004, fibi:0.000011, percent:0.35N=25, fib:0.000004, fibi:0.000012, percent:0.33N=26, fib:0.000004, fibi:0.000011, percent:0.34N=27, fib:0.000004, fibi:0.000011, percent:0.35N=28, fib:0.000004, fibi:0.000012, percent:0.32N=29, fib:0.000004, fibi:0.000012, percent:0.33N=30, fib:0.000004, fibi:0.000013, percent:0.31N=31, fib:0.000004, fibi:0.000012, percent:0.34N=32, fib:0.000004, fibi:0.000012, percent:0.33N=33, fib:0.000004, fibi:0.000013, percent:0.30N=34, fib:0.000004, fibi:0.000012, percent:0.34N=35, fib:0.000004, fibi:0.000013, percent:0.31N=36, fib:0.000004, fibi:0.000013, percent:0.31N=37, fib:0.000004, fibi:0.000014, percent:0.29N=38, fib:0.000004, fibi:0.000014, percent:0.29N=39, fib:0.000004, fibi:0.000013, percent:0.31N=40, fib:0.000004, fibi:0.000014, percent:0.29

Python's Recursive Algorithm Learning (2): Concrete implementation: Fibonacci and the traps therein

Related Article

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: 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.