首页 > 代码库 > 菲波那契数列编程实现
菲波那契数列编程实现
http://blog.csdn.net/pipisorry/article/details/37660419
斐波那契数列
因数学家列昂纳多·斐波那契以兔子生殖为样例而引入,故又称为“兔子数列”。
fibonacci 数列定义:
n = 1,2 时,fib(n) = 1
n > 2 时,fib(n) = fib(n-2) + fib(n-1)
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584,……….
菲波那契数列编程实现
/* 菲波那契数列递归实现 */ int fibonacci(int index){ if( index == 1 || index == 2 ) return 1; return fibonacci(index - 2) + fibonacci(index - 1); } /* 菲波那契数列非递归实现1 */ int fibonacci1(int index){ //1 0 1 1 2 3 5 ... (加入了1 0项) int sum = 0; int pre_pre_sum, pre_sum = 1; while(index--){ pre_pre_sum = pre_sum; //第n-2项的值 pre_sum = sum; //第n-1项的值 sum = pre_sum + pre_pre_sum; //第n项的值 } return sum; } /* 菲波那契数列非递归实现2 */ int fibonacci2(int index){ //-1 1 0 1 1 2 3 5 ... (加入了1 0项) int pre_pre_sum = -1, pre_sum = 1; while(index--){ pre_sum = pre_sum + pre_pre_sum; //第n-1项的值 pre_pre_sum = pre_sum - pre_pre_sum;//第n-2项的值 } return pre_sum + pre_pre_sum; //第n项的值 }
菲波那契数列的还有一种数学展示Made to Order
Divide the number 999,999,999,999,999,999,999,998,999,999,999,999,999,999,999,999 into 1 and express the result as a decimal expansion, and you’ll find the Fibonacci sequence presented in tidy 24-digit strings:
[http://www.futilitycloset.com/2015/06/28/made-to-order-4/]
from:http://blog.csdn.net/pipisorry/article/details/37660419
ref:斐波那契数列(Fibonacci)递归与非递归的性能对照
斐波那契数列非递归算法(fibonacci)
菲波那契数列编程实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。