In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Python recursion and generator to achieve Fibonacci series what is the difference", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "Python recursion and generator to achieve Fibonacci series what is the difference?"
Preface
Fibonacci series, also known as golden section series, also known as rabbit series.
In popular terms, starting from the third term in the series, the value of each subsequent number is equal to the sum of the first two numbers.
And how do we implement Fibonacci sequences of different lengths through python?
Common implementation methods are recursion and generator.
Recursion
The recursive method is inefficient and there will be a large number of double calculations. Here, 20 Fibonacci numbers are taken as an example.
Def fbnq_one (self): if self = = 1: return 1 elif self = = 2: return 2 else: return fbnq_one (self-2) + fbnq_one (self-1) print ([fbnq_one (I) for i in range (1,21)]) generator
The generator needs to use yield in the method. The generator is an iterable object that can traverse to get elements. When getting more Fibonacci series, it is more efficient than recursion. Here we take 100 Fibonacci numbers as an example.
Def fbnq_two (max_num): a, b = 0,1 time_num = 0 while time_num < max_num: yield an a, b = b, aquib time_num + = 1print ([i for i in fbnq_two (100)]) comparison
The recursive syntax is simple, but when executed, there are many repeated calculations, and when the value is large, the running time becomes longer.
The generator can traverse the get elements, and when it gets more Fibonacci sequences, it is more efficient and runs faster than recursion.
Thank you for your reading, the above is "what is the difference between Python recursion and generator Fibonacci series". After the study of this article, I believe you have a deeper understanding of the difference between Python recursion and generator Fibonacci series, and the specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.