菲波那契数列编程实现
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
posted on 2017-04-21 17:48 gavanwanggw 阅读(235) 评论(0) 编辑 收藏 举报