04 Rabbits and Recurrence Relations
Problem
A sequence is an ordered collection of objects (usually numbers), which are allowed to repeat. Sequences can be finite or infinite. Two examples are the finite sequence (π,−2–√,0,π)(π,−2,0,π) and the infinite sequence of odd numbers (1,3,5,7,9,…)(1,3,5,7,9,…). We use the notation anan to represent the nn-th term of a sequence.
A recurrence relation is a way of defining the terms of a sequence with respect to the values of previous terms. In the case of Fibonacci's rabbits from the introduction, any given month will contain the rabbits that were alive the previous month, plus any new offspring. A key observation is that the number of offspring in any month is equal to the number of rabbits that were alive two months prior. As a result, if FnFn represents the number of rabbit pairs alive after the nn-th month, then we obtain the Fibonacci sequence having terms FnFn that are defined by the recurrence relation Fn=Fn−1+Fn−2Fn=Fn−1+Fn−2 (with F1=F2=1F1=F2=1 to initiate the sequence). Although the sequence bears Fibonacci's name, it was known to Indian mathematicians over two millennia ago.
When finding the nn-th term of a sequence defined by a recurrence relation, we can simply use the recurrence relation to generate terms for progressively larger values of nn. This problem introduces us to the computational technique of dynamic programming, which successively builds up solutions by using the answers to smaller cases.
Given: Positive integers n≤40n≤40 and k≤5k≤5.
Return: The total number of rabbit pairs that will be present after nn months, if we begin with 1 pair and in each generation, every pair of reproduction-age rabbits produces a litter of kk rabbit pairs (instead of only 1 pair).
Sample Dataset
5 3
Sample Output
19 注: F1 = 1, F2 = 1, F3 = F2+F1*2,
这里k=3, 所以手写答案为 1,1,4,7,
方法一: def fibonacciRabbits(n, k):
F = [1, 1]
generation = 2
while generation <= n:
F.append(F[generation - 1] + F[generation - 2] * k)
generation += 1
return (F[n-1]) print fibonacciRabbits(5, 3) 方法二: def fibonacciRabbits(n,k):
if n <= 2:
return (1)
else:
return (fibonacciRabbits(n-1,k) + fibonacciRabbits(n-2,k)*k)
print fibonacciRabbits(5,3)
04 Rabbits and Recurrence Relations的更多相关文章
- 4.Rabbits and Recurrence Relations
Problem A sequence is an ordered collection of objects (usually numbers), which are allowed to repea ...
- 11 Mortal Fibonacci Rabbits
Problem Figure 4. A figure illustrating the propagation of Fibonacci's rabbits if they die after thr ...
- Maple拥有优秀的符号计算和数值计算能力
https://www.maplesoft.com/products/maple/ Maple高级应用和经典实例: https://wenku.baidu.com/view/f246962107221 ...
- Python高级特性(1):Iterators、Generators和itertools(参考)
对数学家来说,Python这门语言有着很多吸引他们的地方.举几个例子:对于tuple.lists以及sets等容器的支持,使用与传统数学类 似的符号标记方式,还有列表推导式这样与数学中集合推导式和集的 ...
- POJ #2479 - Maximum sum
Hi, I'm back. This is a realy classic DP problem to code. 1. You have to be crystal clear about what ...
- Master Theorem
Master theorem provides a solution in asymptotic terms to solve time complexity problem of most divi ...
- [LeetCode] Cherry Pickup 捡樱桃
In a N x N grid representing a field of cherries, each cell is one of three possible integers. 0 mea ...
- 004_Python高级特性(1):Iterators、Generators和itertools(参考)
对数学家来说,Python这门语言有着很多吸引他们的地方.举几个例子:对于tuple.lists以及sets等容器的支持,使用与传统数学类 似的符号标记方式,还有列表推导式这样与数学中集合推导式和集的 ...
- IEEEXtreme Practice Community Xtreme9.0 - Digit Fun!
Xtreme9.0 - Digit Fun! 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/di ...
随机推荐
- ping第一包时间过长
一.现象 公司互联网域(说白了就是可以连外网的vlan区域的主机)的几台主机在ping一个合作方提供的域名时,发现在ping 第一个包时,时间特别长,后面第2--N包开始正常.直接ping 该域名对应 ...
- 老师金角大王的BLOG
首页: http://www.cnblogs.com/alex3714/ python3专题: http://www.cnblogs.com/alex3714/category/770733.html
- ALSA声卡08_从零编写之框架_学习笔记
1.整体框架 (1)图示((DAI(全称Digital Audio Interface)接口)) 在嵌入式系统里面,声卡驱动是ASOC,是在ALSA驱动上封装的一层,包括以下三大块 (2)程序框架 m ...
- css3的transition属性的使用
transition是将某个属性从一个属性值在指定的时间内平滑过渡到另一个属性值来实现动画效果.这个属性一般搭配:hover来使 下面看一个例子:鼠标放在div上,0.2s后将div元素的背景色用一秒 ...
- 5月3日上课笔记-XML解析
一.XML编程 1.xml编程的两种解析方式 1.1 dom解析 优点:一次加载,多次使用.可以方便的对xml文档进行增删改查 缺点:如果xml文档过大的话,加载的时候会比较占用内存空间比较大,消耗资 ...
- 如何利用R包qqman画曼哈顿图?
如何利用R包qqman画曼哈顿图? 2017-07-10 lili 生信人 众多周知,R语言提供了各种各样的包,方便实现我们的目的,下面给大家介绍一个可以便捷的画曼哈顿图的包:qqman instal ...
- Setup FTP server on Ubuntu 14.04
Setup FTP server on Ubuntu 14.04 Step 1 » Update repositories .krizna@leela:~$ sudo apt-get updateSt ...
- 【转】C#事件、委托、回调的用法与区别
原文地址:http://blog.csdn.net/huang9012/article/details/38753305 事件包括:事件发送者.事件接收者和事件处理程序.关于事件,我们首要知道的是事件 ...
- 数据库设计不推荐使用Bool类型
参见博文:http://blog.csdn.net/zhang_xinxiu/article/details/8521673
- python开发_python关键字
python3.3.2中的关键字如下: The following identifiers are used as reserved words, or keywords of the languag ...