菲波那契数列编程实现

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:

fibonacci expansion

[http://www.futilitycloset.com/2015/06/28/made-to-order-4/]

from:http://blog.csdn.net/pipisorry/article/details/37660419

ref:斐波那契数列(Fibonacci)递归与非递归的性能对比

斐波那契数列非递归算法(fibonacci)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值