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 ...
随机推荐
- Java运算符,算术运算符
算术运算符介绍 算术运算符用在数学表达式中,它们的作用和在数学中的作用一样. 下表列出了所有的算术运算符. 表格中的实例假设整数变量A的值为10,变量B的值为20: 操作符 描述 例子 + 加法 - ...
- [Java][Web]解决 Request 的乱码
解决 get 提交的乱码 (手工处理) String username = request.getParameter("username"); username = new Str ...
- 第十章 hbase默认配置说明
hbase.rootdir:这个目录是region server的共享目录,用来持久化Hbase.URL需要是'完全正确'的,还要包含文件系统的scheme.例如,要表示hdfs中的 '/hbase ...
- nfs之脚气
nfs工作模式参考 一.NFs是什么 NFS是Network File System的缩写,即网络文件系统.客户端通过挂载的方式将NFS服务器端共享的数据目录挂载到本地目录下. nfs为什么需要RPC ...
- 很漂亮的按钮css样式(没有用到图片,可直接拷贝代码使用)
[转自]http://blog.csdn.net/lushuaiyin/article/details/8118669 对于程序员,有时候也需要对页面风格做些改动,整体的页面风格还是美工的工作. 按钮 ...
- Python Object Oriented
1. Creating class class className: 'Optional class documentation string' class_suite The class has a ...
- js中的web加密
js中的web加密 window.crypto.subtle只会在安全模式下有用,也就是https环境下 创建摘要(硬解) var i = new TextEncoder('utf-8').encod ...
- Foglight 5.6.7 控制台jboss报404
Foglight 5.6.7 控制台jboss报404 [终极问题原来就一数据库权限问题!下文是由问题源找到的类似的dell support的相关文章,但却是关于sql server error的,感 ...
- 数据分析之Numpy-数组计算
引言 : 数据分析 : 就是把隐藏在一些看似杂乱无章的数据背后的信息提炼出来,总结出研究对象的内在规律 . 数据分析三剑客 : Numpy 数组计算 Pandas 表计算与数据分析 ...
- krpano资源下载及还原全景图
krpano资源下载及还原全景图 现在全景云平台有大量的全景图资源,就存在了如何下载的需求. 原理:1.云平台多数使用krpano内核,首先需要将全景云平台中被krpano切成的全景图碎片下载下来,并 ...