python实现斐波那契递归函数的方法

WBOY
Release: 2016-06-16 08:42:12
Original
1436 people have browsed it

本文以一个简单的实例讲述了python实现斐波那契数列数列递归函数的方法,代码精简易懂。分享给大家供大家参考之用。

主要函数代码如下:

def fab(n):
  if n==1:
    return 1
  if n==0:
    return 0
  else:
    result=int(fab(n-1))+int(fab(n-2))    
    return result

Copy after login

测试代码如下:

for i in range(10):
  print fab(i)

Copy after login

希望本文所述对大家Python程序设计的学习有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!