Four ways to do the Fibonacci sequence in Python

Source: Internet
Author: User

At these times, I can laugh and the project manager never blames him. And the project manager met Kong Yiji, also often ask him, to arouse laughter. Kong Yiji himself knew that he could not talk with them, he had to speak to the newcomer. Once said to me, "Have you learned the data structure?" "I'll have a little bit of a head. He said, "Learning about data structures, ... I will test you a test. What does the Fibonacci sequence say in Python? "I suppose, begging the same person, also test me?" Then went back to the face, no longer listen. Kong Yiji waited for a long time, very earnestly said, "Can't write?" ...... I'll teach you, remember! These words should be remembered. When you are a project manager in the future, write your account. "I thought to myself that I was still a long way from the project manager, and that we could not use the Fibonacci sequence in our project," said the funny, impatient, lazy answer, "Who wants you to teach, not f (n) = f (n-1) +f (n-2)?" Kong Yiji was so happy that he knocked two fingers on his desk and nodded, "Yes, yes!" ...... The Fibonacci sequence has four ways of writing in Python, you know? The more impatient I was, the more I tried to walk away. Kong Yiji just took out a notebook computer, want to write a few procedures, see I was not enthusiastic, then sighed again, showing a very sorry look.

The definition of the Fibonacci sequence

F (0) = 1,f (1) = 1,f (n) = f (n-1) + f (n-2)

CODE

This article introduces the four kinds of the Fibonacci sequence in Python, the first one is more common, the second is more common. (Lu Xun tried to beat people up). The first one relies on recursion, the second dependency and loop, both of which can be quickly ported in almost any programming language. Let's start with both of these introductions.

The first type: recursion
# recursiveDefFibonacci_recursion_tool(n):if n <= 0: return 0 elif n = = 1: return 1 else: return Fibonacci_recursion_tool (n- 1) + fibonacci_recursion_tool (n- 2)def fibonacci_recursion(n): Result_list = [] for I in range (1, n + 1): Result_list.append (Fibonacci_recursion_tool (i)) return result_list 
Performance comparison

Here we use the time function for timing. And save to a file using the NumPy class library

DefTest_fibonacci(n, list): T1 = Time.clock () fibonacci_recursion (n) t2 = Time.clock () L1 = t2-t1 T1 = time.clock () Fib Onacci_loop (n) t2 = Time.clock () L2 = t2-t1 T1 = time.clock () fibonacci_yield (n) t2 = Time.clock () L3     = T2-t1 T1 = time.clock () fibonacci_matrix (n) t2 = Time.clock () L4 = T2-t1 List.append ([L1,l2, L3, L4]) Print"The test results for%d times are:"% n, [L1,l2, L3, L4])def test_save  (times_items, filename): Times_list = [] for I in Range (1, Times_items + 1): Test_fibonacci (i, times_list) np.savetxt (filename, times_list) def test_print (test_print_n): Print (Fibonacci_recursion (test_print_n)) print (Fibonacci_loop (Test_print_n)) print (Fibonacci_yield (test_print_n)) print (Fibonacci_matrix (test_print_n)) Times_ Items = 40filename =  "/home/fonttian/data/17_ds_ai/fibonacci/ Fibonacci_all.txt "# test_save (times_items,filename)     

From the effect of the first one of the worst effect in more than 35 of the number of operations, time will reach 1s, while the other computing speed is still at ten negative five to negative six between the parties, when the number of times greater than 1000, loop speed is obviously insufficient.

In the POW (10000,10000), the matrix and yield are computed at a rate of

0.278406000000000041.6000000000016e-05

It seems that yield performance is better, but it is not, because yield and NP. Matrix In fact the operation mechanism caused, in fact, in a large number of operations, there is a problem, yield is actually no operation? NP. A memory overflow (resulting in a numeric error) occurred in the Matrix

Overall, however, the best is yield, which is the credit for Python's excellent design.

Four ways to do the Fibonacci sequence in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.