python的递归算法学习(1)
递归函数
在函数内部,可以调用其他函数。如果一个函数在内部调用自身本身,这个函数就是递归函数。
举个例子,我们来计算阶乘 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)的更多相关文章
- python的递归算法学习(3):汉诺塔递归算法
汉诺塔问题是递归函数的经典应用,它来自一个古老传说:在世界刚被创建的时候有一座钻石宝塔A,其上有64个金蝶.所有碟子按从大到小的次序从塔底堆放至塔顶.紧挨着这座塔有另外两个钻石宝塔B和C.从世界创始之 ...
- python的递归算法学习(2):具体实现:斐波那契和其中的陷阱
1.斐波那契 什么是斐波那契,斐波那契额就是一个序列的整数的排序,其定义如下: Fn = Fn-1 + Fn-2 with F0 = 0 and F1 = 1 也就是,0,1,1,2,3,5,8,13 ...
- Python 装饰器学习
Python装饰器学习(九步入门) 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 1 2 3 4 5 6 7 8 # -*- c ...
- Requests:Python HTTP Module学习笔记(一)(转)
Requests:Python HTTP Module学习笔记(一) 在学习用python写爬虫的时候用到了Requests这个Http网络库,这个库简单好用并且功能强大,完全可以代替python的标 ...
- 从Theano到Lasagne:基于Python的深度学习的框架和库
从Theano到Lasagne:基于Python的深度学习的框架和库 摘要:最近,深度神经网络以“Deep Dreams”形式在网站中如雨后春笋般出现,或是像谷歌研究原创论文中描述的那样:Incept ...
- Comprehensive learning path – Data Science in Python深入学习路径-使用python数据中学习
http://blog.csdn.net/pipisorry/article/details/44245575 关于怎么学习python,并将python用于数据科学.数据分析.机器学习中的一篇非常好 ...
- (转载)Python装饰器学习
转载出处:http://www.cnblogs.com/rhcad/archive/2011/12/21/2295507.html 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方 ...
- python网络爬虫学习笔记
python网络爬虫学习笔记 By 钟桓 9月 4 2014 更新日期:9月 4 2014 文章文件夹 1. 介绍: 2. 从简单语句中開始: 3. 传送数据给server 4. HTTP头-描写叙述 ...
- Python装饰器学习
Python装饰器学习(九步入门) 这是在Python学习小组上介绍的内容,现学现卖.多练习是好的学习方式. 第一步:最简单的函数,准备附加额外功能 ? 1 2 3 4 5 6 7 8 # -*- ...
随机推荐
- 3,MongoDB之数据类型
一.MongoDB 之数据类型 首先我们要先了解一下MongoDB中有什么样的数据类型: Object ID :Documents 自生成的 _id String: 字符串,必须是utf-8 Boo ...
- 1 HTML + CSS
1.HTML的基础用法 2.标签的嵌套 3.常见的网页结构 header content footer
- android gradle.properties
gradle.properties 里面配置的东西,在gradle 文件里面可以直接引用. 例如: 在你工程根目录的gradle.properties 文件里面 可以这样配置: ## Project- ...
- android:windowBackground 和 Android:background 的区别
通过问别人,我知道了android:windowBackground 和 Android:background的区别 android:windowBackground 一般用于activity启动的时 ...
- Java基础-3类和对象声明与创建
一).在1和2中有粗略介绍过类和对象的概念,在这里简单回顾一下: 对象与类:一个实际或者虚拟的物体,这个物体既是我们的对象,这个物体呢又是属于一个分类,如动物类,人类 二).创建对象: 在创建对象的时 ...
- Python面试题(练习三)
1.MySQL索引种类 1.普通索引 2.唯一索引 3.主键索引 4.组合索引 5.全文索引 2.索引在什么情况下遵循最左前缀的规则? 最左前缀原理的一部分,索引index1:(a,b,c),只会走a ...
- ctags+cscope替换sourceinsight
背景 windows环境开发+linux交叉编译的开发模式,代码阅读和编写都用的source-insight. 除了检索,跳转,工程构建等方面,sourceinsight自带了一些宏语言,可以轻松实现 ...
- 搭建 MongoDB分片(sharding) / 分区 / 集群环境
1. 安装 MongoDB 三台机器 关闭防火墙 systemctl stop firewalld.service 192.168.252.121 192.168.252.122 192.168.25 ...
- 思梦PHP-阿里大鱼手机验证码
小伙伴是否做PC网站的时候,是否遇到过注册用户需要使用短信验证的功能呢?或者找回密码,以及验证用户的信息等等功能!今天思梦PHP就为大家带来ThinkPHP整合阿里大鱼短信验证的功能! 首先,我们要明 ...
- group_load,weight_load,group_capacity, group_weight大致都是啥数值
[ 113.180820] sgs->group_load:2039,sum_nr_running:2,sum_weighted_load:2039,sgs->group_capacity ...