Home Backend Development Python Tutorial How to implement the Fibonacci function using Python

How to implement the Fibonacci function using Python

Mar 10, 2017 pm 01:58 PM
python

This article mainly introduces how to use Python to implement Fibonacci function related information. Friends who need it can refer to

Fibonacci Fibonacci sequence. It is very simple. It is just a recursion. Learn Any programming language can probably do this.

I have been playing Python recently. After a cursory look at Learning Python and Core Python, I accidentally found a post on the Internet about the evolution of Python programmers. It was very interesting. So I plan to imitate a post that uses more than ten methods to complete a factorial function. Here I will write a Fibonacci function in nine different styles.

The requirements are very simple, input n, output the nth Fibonacci number, n is a positive integer

The following are the nine different styles:

1) Python programmers who write programs for the first time:

def fib(n):
  return nth fibonacci number
Copy after login

Explanation:
People who write programs for the first time tend to follow human The syntax of a language rather than the syntax of a programming language. Take a buddy of mine who is very good at programming as an example. The first program he wrote to determine a leap year directly stated this: If year is a leap year, the output year is a leap year. Otherwise year is not a leap year.

2) C programmers who have just learned Python:

def fib(n):#{
 if n<=2 :
  return 1;
 else:
  return fib(n-1)+fib(n-2);
#}
Copy after login

Explanation:
When I first came into contact with Python , I am very uncomfortable with using indentation instead of braces to divide program blocks, and there is no terminator after each statement, so the first thing I do after writing a Python function is usually to Comment out the braces and add missing colons.

3) Lazy Python programmer:

def fib(n):
  return 1 and n<=2 or fib(n-1)+fib(n-2)
Copy after login

Explanation:
I didn’t know Python until I watched Learning Python No ternary operator? , but given that the bool value in Python is quite special (a bit like C, non-zero means true, non-empty means true), and Python's logical statements also support short-circuit evaluation (Short-Circuit Evaluation), this can be written An imitation? statement comes out.

4) Lazier Python programmers:

 fib=lambda n:1 if n<=2 else fib(n-1)+fib(n-2)
Copy after login

Description:
lambda keyword I have used in C# and Scheme However, lambda in Python is simpler than in C# and is very similar to the usage in Scheme, so I quickly adapted to it. This way of writing is often used when declaring some small functions in the Python Shell.

5) Python programmers who have just learned data structures:

def fib(n):
 x,y=0,1
 while(n):
  x,y,n=y,x+y,n-1
 return x
Copy after login

Explanation:
The previous Fibonacci functions are all It is an implementation of tree recursion. Even if you learn a little algorithm, you should know the inefficiency of this kind of recursion. Here, changing from tree recursion to corresponding iteration can improve the efficiency a lot.
Python's tuple assignment feature is something I like very much. This thing can simplify the code a lot. For example, the previous tmp=a;a=b;b=tmp; can be directly implemented with the sentence a,b=b,a, which is both concise and clear.

6) Python programmers who are taking SICP courses:

def fib(n):
  def fib_iter(n,x,y):
   if n==0 : return x
   else : return fib_iter(n-1,y,x+y)

  return fib_iter(n,0,1)
Copy after login

Note:
Here I used Scheme Tail-recursion is a very common writing method in languages. There is no iteration in Scheme, but invariants and tail recursion can be used to simulate iteration to achieve the same effect. However, I still don’t know if Python has made corresponding optimizations for tail recursion. I will check back.
PS: Students who have read SICP can tell at a glance that this program is actually an example in Chapter 1 of SICP.

7) A clever Python programmer:

fib=lambda n,x=0,y=1:x if not n else f(n-1,y,x+y)
Copy after login

Instructions:
Basic logic and the above example The same, both are written in tail recursive way. The main difference is that it uses the default parameters and ternary operator provided by Python, thereby simplifying the code to one line. As for default parameters, students who have studied C++ all know this stuff, and C# 4.0 has also introduced this stuff.

8) Python programmers who have just completed linear algebra:

def fib(n):
 def m1(a,b):
  m=[[],[]]
  m[0].append(a[0][0]*b[0][0]+a[0][1]*b[1][0])
  m[0].append(a[0][0]*b[0][1]+a[0][1]*b[1][1])
  m[1].append(a[1][0]*b[0][0]+a[1][1]*b[1][0])
  m[1].append(a[1][0]*b[1][0]+a[1][1]*b[1][1])
  return m
 def m2(a,b):
  m=[]
  m.append(a[0][0]*b[0][0]+a[0][1]*b[1][0])
  m.append(a[1][0]*b[0][0]+a[1][1]*b[1][0])
  return m
 return m2(reduce(m1,[[[0,1],[1,1]] for i in range(n)]),[[0],[1]])[0]
Copy after login

Explanation:
This code is not It is as clear as the previous code, so I will introduce the principle first (requiring a little knowledge of linear algebra):
First look at the previous iterative version of the Fibonacci function, it is easy to find that there is a transformation: y->x, x +y->y. From another angle, it is [x,y]->[y,x+y].
Here, I declare a binary vector [x,y]T, which obtains [y,x+y]T through a transformation. It can be easily obtained that the transformation matrix is ​​[[1,0],[1, 1]], that is to say: [[1,0],[1,1]]*[x,y]T=[y,x+y]T
Let the binary matrix A=[[1, 0],[1,1]], binary vector x=[0,1]T, it is easy to know that the result of Ax is the next Fibonacci value, that is:
Ax=[fib(1),fib(2) ]T
also has:
Ax=[fib(2),fib(3)]T
………………
By analogy, we can get:

Aⁿx=[fib(n),fib(n-1)]T
Copy after login

That is to say, you can perform n A transformations on the binary vector [0,1]T to get [fib(n),fib(n+1 )]T, thus obtaining fib(n).

Here I define a binary matrix multiplication function m1, and a transformation m2 on a binary vector, and then use the reduce operation to complete a continuous multiplication operation to get Aⁿx, and finally get fib (n).

9) Python programmers preparing to participate in the ACM competition:

 def fib(n):
 lhm=[[0,1],[1,1]]
 rhm=[[0],[1]]
 em=[[1,0],[0,1]]
 #multiply two matrixes
 def matrix_mul(lhm,rhm):
  #initialize an empty matrix filled with zero
  result=[[0 for i in range(len(rhm[0]))] for j in range(len(rhm))]
  #multiply loop
  for i in range(len(lhm)):
   for j in range(len(rhm[0])):
    for k in range(len(rhm)):
     result[i][j]+=lhm[i][k]*rhm[k][j]
  return result
 
 def matrix_square(mat):
  return matrix_mul(mat,mat)
 #quick transform
 def fib_iter(mat,n):
  if not n:
   return em
  elif(n%2):
   return matrix_mul(mat,fib_iter(mat,n-1))
  else:
   return matrix_square(fib_iter(mat,n/2))
 return matrix_mul(fib_iter(lhm,n),rhm)[0][0]
Copy after login

Instructions:

看过上一个fib函数就比较容易理解这一个版本了,这个版本同样采用了二元变换的方式求fib(n)。不过区别在于这个版本的复杂度是lgn,而上一个版本则是线性的。

这个版本的不同之处在于,它定义了一个矩阵的快速求幂操作fib_iter,原理很简单,可以类比自然数的快速求幂方法,所以这里就不多说了。

PS:虽然说是ACM版本,不过说实话我从来没参加过那玩意,毕竟自己算法太水了,那玩意又太高端……只能在这里YY一下鸟~

python中,最基本的那种递归(如下fib1)效率太低了,只要n数字大了运算时间就会很长;而通过将计算的指保存到一个dict中,后面计算时直接拿来使用,这种方式成为备忘(memo),如下面的fib2函数所示,则会发现效率大大提高。

在n=10以内时,fib1和fab2运行时间都很短看不出差异,但当n=40时,就太明显了,fib1运行花了35秒,fab2运行只花费了0.00001秒。
n=40时,输出如下:

jay@jay-linux:~/workspace/python.git/py2014$ python fibonacci.py 
2014-10-16 16:28:35.176396
fib1(40)=102334155
2014-10-16 16:29:10.479953
fib2(40)=102334155
2014-10-16 16:29:10.480035
Copy after login

这两个计算Fibonacci数列的函数,如下:https://github.com/smilejay/python/blob/master/py2014/fibonacci.py

import datetime

def fib1(n):
  if n == 0:
    return 0
  elif n == 1:
    return 1
  else:
    return fib1(n - 1) + fib1(n - 2)
 
known = {0: 0, 1: 1}
 
def fib2(n):
  if n in known:
    return known[n]
 
  res = fib2(n - 1) + fib2(n - 2)
  known[n] = res
  return res

if __name__ == &#39;__main__&#39;:
  n = 40
  print(datetime.datetime.now())
  print(&#39;fib1(%d)=%d&#39; % (n, fib1(n)))
  print(datetime.datetime.now())
  print(&#39;fib2(%d)=%d&#39; % (n, fib2(n)))
  print(datetime.datetime.now())
Copy after login

后记:

由于刚学习Python没多久,所以对其各种特性的掌握还不够熟练。与其说是我在用Python写程序,倒不如说我是在用C,C++,C#或是Scheme来写程序。至于传说中的Pythonic way,我现在还没有什么体会,毕竟还没用Python写过什么真正的程序。
Learning Python和Core Python都是不错的Python入门书籍,前者更适合没有编程基础的人阅读。
Python是最好的初学编程入门语言,没有之一。所以它可以取代Scheme成为MIT的计算机编程入门语言。

The above is the detailed content of How to implement the Fibonacci function using Python. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Python vs. JavaScript: Community, Libraries, and Resources Python vs. JavaScript: Community, Libraries, and Resources Apr 15, 2025 am 12:16 AM

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

Detailed explanation of docker principle Detailed explanation of docker principle Apr 14, 2025 pm 11:57 PM

Docker uses Linux kernel features to provide an efficient and isolated application running environment. Its working principle is as follows: 1. The mirror is used as a read-only template, which contains everything you need to run the application; 2. The Union File System (UnionFS) stacks multiple file systems, only storing the differences, saving space and speeding up; 3. The daemon manages the mirrors and containers, and the client uses them for interaction; 4. Namespaces and cgroups implement container isolation and resource limitations; 5. Multiple network modes support container interconnection. Only by understanding these core concepts can you better utilize Docker.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Python: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

What is vscode What is vscode for? What is vscode What is vscode for? Apr 15, 2025 pm 06:45 PM

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages ​​and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

How to install nginx in centos How to install nginx in centos Apr 14, 2025 pm 08:06 PM

CentOS Installing Nginx requires following the following steps: Installing dependencies such as development tools, pcre-devel, and openssl-devel. Download the Nginx source code package, unzip it and compile and install it, and specify the installation path as /usr/local/nginx. Create Nginx users and user groups and set permissions. Modify the configuration file nginx.conf, and configure the listening port and domain name/IP address. Start the Nginx service. Common errors need to be paid attention to, such as dependency issues, port conflicts, and configuration file errors. Performance optimization needs to be adjusted according to the specific situation, such as turning on cache and adjusting the number of worker processes.

See all articles