递归函数
在函数内部,可以调用其他函数。如果一个函数在内部调用自身本身,这个函数就是递归函数。
举个例子,我们来计算阶乘 n! = 1 * 2 * 3 * ... * n,用函数 fact(n)表示,可以看出:
fact(n) = n! = 1 * 2 * 3 * ... * (n-1) * n = (n-1)! * n = fact(n-1) * n
所以,fact(n)可以表示为 n * fact(n-1),只有n=1时需要特殊处理。
于是,fact(n)用递归的方式写出来就是:

def fact(n):
if n==1:
  return 1
return n * fact(n - 1)

如果要计算2的n次方,那就是:

def fact(n):
if n==1:
  return 1
return 2 * fact(n - 1)

我们可以修改一下代码,详细的列出每一步(注意打印出来的内容的顺序哦):

def fact(n):
print("factorial has been called with n = " + str(n))
if n == 1:
return 1
else:
res = n * fact(n - 1)
print("intermediate result for ", n, " * fact(", n - 1, "): ", res)
return res print(fact(10))

结果是:

C:\Python35\python.exe C:/pylearn/bottlelearn/4.py
factorial has been called with n = 10
factorial has been called with n = 9
factorial has been called with n = 8
factorial has been called with n = 7
factorial has been called with n = 6
factorial has been called with n = 5
factorial has been called with n = 4
factorial has been called with n = 3
factorial has been called with n = 2
factorial has been called with n = 1
intermediate result for 2 * fact( 1 ): 2
intermediate result for 3 * fact( 2 ): 6
intermediate result for 4 * fact( 3 ): 24
intermediate result for 5 * fact( 4 ): 120
intermediate result for 6 * fact( 5 ): 720
intermediate result for 7 * fact( 6 ): 5040
intermediate result for 8 * fact( 7 ): 40320
intermediate result for 9 * fact( 8 ): 362880
intermediate result for 10 * fact( 9 ): 3628800
1814400000 Process finished with exit code 0

进一步思考,如果,我们想实现递归的效果,但是,却不想用到递归,在python怎么实现呢:

def fact(n):
result=1
for i in range(2,n+1):
result=result*i
return result print(fact(1))
print(fact(2))
print(fact(10))

python的递归算法学习(1)的更多相关文章

  1. python的递归算法学习(3):汉诺塔递归算法

    汉诺塔问题是递归函数的经典应用,它来自一个古老传说:在世界刚被创建的时候有一座钻石宝塔A,其上有64个金蝶.所有碟子按从大到小的次序从塔底堆放至塔顶.紧挨着这座塔有另外两个钻石宝塔B和C.从世界创始之 ...

  2. python的递归算法学习(2):具体实现:斐波那契和其中的陷阱

    1.斐波那契 什么是斐波那契,斐波那契额就是一个序列的整数的排序,其定义如下: Fn = Fn-1 + Fn-2 with F0 = 0 and F1 = 1 也就是,0,1,1,2,3,5,8,13 ...

  3. Python 装饰器学习

    Python装饰器学习(九步入门)   这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 1 2 3 4 5 6 7 8 # -*- c ...

  4. Requests:Python HTTP Module学习笔记(一)(转)

    Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...

  5. 从Theano到Lasagne:基于Python的深度学习的框架和库

    从Theano到Lasagne:基于Python的深度学习的框架和库 摘要:最近,深度神经网络以“Deep Dreams”形式在网站中如雨后春笋般出现,或是像谷歌研究原创论文中描述的那样:Incept ...

  6. Comprehensive learning path – Data Science in Python深入学习路径-使用python数据中学习

    http://blog.csdn.net/pipisorry/article/details/44245575 关于怎么学习python,并将python用于数据科学.数据分析.机器学习中的一篇非常好 ...

  7. (转载)Python装饰器学习

    转载出处:http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方 ...

  8. python网络爬虫学习笔记

    python网络爬虫学习笔记 By 钟桓 9月 4 2014 更新日期:9月 4 2014 文章文件夹 1. 介绍: 2. 从简单语句中開始: 3. 传送数据给server 4. HTTP头-描写叙述 ...

  9. Python装饰器学习

    Python装饰器学习(九步入门)   这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 ? 1 2 3 4 5 6 7 8 # -*- ...

随机推荐

  1. [Codeforces Round #443]Div2 C Short Program

    给你一串$n$个按顺序的位运算操作(&,|,^),形如"opt x",要求用不超过5行的位运算,按顺序操作出来和那个结果是一样的.$(n<=5e5,0<=x&l ...

  2. laravel5.5入口文件分析

    入口文件 public/index.php 1.加载composer的自动加载器 require __DIR__.'/../vendor/autoload.php'; 自动加载,不用再各种requir ...

  3. Java - ArrayList List 等迭代集合执行移除(remove) 的正确方法

    方法1: List<String> al = new ArrayList<String>(); Iterator<String> it = al.iterator( ...

  4. ceph 存储系统

    http://w w w.c s d n 123.com/html/topnews201408/2/4702.htm

  5. 《1024伐木累-周末特别篇》-中彩票了,开发APP

    本周发布的<1024伐木累>,受到了很多码汪们的好评,博主在这里感谢大家的支持,同时,博主临时起意,增加一期周末对话特别篇,让大家在“满血复活”的时间里,充分感受快乐的味道~ 1.中彩票 ...

  6. Android学习笔记(一)之仿正点闹钟时间齿轮滑动的效果

    看到正点闹钟上的设置时间的滑动效果非常好看,自己就想做一个那样的,在网上就开始搜资料了,看到网上有的齿轮效果的代码非常多,也非常难懂,我就决定自己研究一下,现在我就把我的研究成果分享给大家.我研究的这 ...

  7. 《Cracking the Coding Interview》——第5章:位操作——题目7

    2014-03-19 06:27 题目:有一个数组里包含了0~n中除了某个整数m之外的所有整数,你要设法找出这个m.限制条件为每次你只能用O(1)的时间访问第i个元素的第j位二进制位. 解法:0~n的 ...

  8. PyCharm 解决有些库(函数)没有代码提示

    问题描述: 如图,当输入 im. 没有智能提示第三库相应的函数或其他提示. 解决方案: python是动态强类型语言,IDE无法判断Image.open("panda.png")的 ...

  9. 4.实现简单的shell sed替换功能

    # -*- coding:utf-8 -*- # Author: JACK ZHAO # 程序1: 实现简单的shell sed替换功能 import sys #判断参数个数 if len(sys.a ...

  10. 课时34:丰富的else语句以及简洁的with语句

    目录: 一.丰富的else语句 二.简洁的with语句 三.课时34课后习题及答案 *********************** 一.丰富的else语句 ********************** ...