---
title: "Fibonacci number -Python"
author: "zzysh"
date: "2008-03-24T22:40:09Z"
tags: []
slug: "fibonacci-number-python"
draft: false
lastmod: "2008-03-24T22:40:09Z"
id: 175601
url: "https://elton.iteye.com/blog/175601"
---

```python

# -*- coding: UTF-8 -*-
#递归算法
def recursion(n):
	if (n<1):
		return 0
	elif (n<3):
		return 1
	else:
		return recursion(n-1)+recursion(n-2)

#循环算法
def loop(n):
	print 1,1,
	a,b = 1,2
	i = 2
	while i <= n:
		print b,
		a,b = b, a+b
		i += 1
	

if __name__=="__main__":
	print "递归算法"
	for i in range(1,16):
		print recursion(i),
	print "\n"+'-'*50
	
	print "迭代法"
	loop(14)

```