Rabbit issues of interesting algorithms

Source: Internet
Author: User
Assume that you have a pair of male and female rabbits that have just been born. they start to mate when they are a month old. at the end of November February, the female rabbits will produce another one, after a month, they began to breed, so they continued. Each female produces a rabbit every month when it begins to breed. If no rabbit died, how many rabbits will there be in total after one year? At the end of January, the first couple of rabbits mate, but there were only one pair of rabbits.

Assume that you have a pair of male and female rabbits that have just been born. they start to mate when they are a month old. at the end of November February, the female rabbits will produce another one, after a month, they began to breed, so they continued. Each female produces a rabbit every month when it begins to breed. If no rabbit died, how many rabbits will there be in total after one year?

At the end of January, the first couple of rabbits mate, but there were only one pair of rabbits. at the end of February, the female rabbits had two rabbits. at the end of March, the oldest female rabbits gave birth to the second, with a total of three rabbits. at the end of April, the oldest female rabbits gave birth to the third rabbit, and the female rabbits born two months ago gave birth to the next rabbit, A total of 5 rabbits ;...... In this case, the rabbit logarithm is: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,... do you see the rule? Starting from 3rd, each number is the sum of the first two. This is the famous Fibonacci series.

C programming solution:

# Include
 
  
Int Fib (int n) {if (n = 1 | n = 2) return 1; elsereturn Fib (n-1) + Fib (n-2);} int main () {int wait; printf ("% d can be set to rabbit n in a year", Fib (12); scanf ("% d", & wait); return 0 ;}
 

There are many related questions:

There are 10 levels of stairs. each step can only span one or two levels. how many different steps are there to climb 10th levels?

A: This is a Fibonacci series: there is a method of boarding the first level; there are two methods of boarding the second level; three methods of boarding the third level; there are five methods ...... So, 1, 2, 3, 5, 8, 13 ...... Grade 10, 89 types.

What is the limit of the two adjacent items in the series compared to the latter? I am wondering, what is the limit of F (n)/F (n + 1) when n tends to be infinitely greater?

A: This can be obtained directly from its generic formula. the limit is (-1 + √ 5)/2. this is the so-called Golden splitting point and a number representing the harmony of nature.

The mathematical expression of the Fibonacci series is F (n) = F (n-1) + F (n-2), F (1) = 1, F (2) = 1.

The Fibonacci series can be written using a binary recursive program. The C ++ language is described as follows:

long fib1(int n){if (n <= 2){   return 1;}else{   return fib1(n-1) + fib1(n-2);}}

It seems that the recursive use of the program is very appropriate, but it takes about 3 s to test n = 37 in the VC2005 environment, at the time of n = 45, there was no result after I went downstairs to finish the meal ...... Obviously, this recursion is too inefficient.

For example, use the following test function:

long fib1(int n, int* arr){arr[n]++;if (n <= 2){ return 1;}else {  return fib1(n-1, arr) + fib1(n-2, arr);}}

In this case, we can obtain the number of times each fib (I) is calculated:

fib(10) = 1     fib(9) = 1      fib(8) = 2      fib(7) = 3fib(6) = 5      fib(5) = 8      fib(4) = 13    fib(3) = 21fib(2) = 34   fib(1) = 55    fib(0) = 34

It can be seen that the number of computations is in the back-to-back Fibonacci series, which clearly results in a large number of repeated computations.

We set T (N) as the running time of function fib (n). when N> = 2, we can see that T (N) = T (N-1) + T (N-2) + 2

In fact, this violates a recursive rule: the synthetic benefit rule.

Compound interest rule: when solving the same instance of a problem, do not perform repetitive work in different recursive calls. So when calling fib (N-1) in the code above, we actually calculated fib (N-2) at the same time ). This small repetitive computation will generate a huge running time in the recursive process.

The approximate linear efficiency can be achieved using a cross-recursive program. The C ++ language is described as follows:

long fib(int n, long a, long b, int count){     if (count == n)         return b;     return fib(n, b, a+b, ++count);} long fib2(int n){     return fib(n, 0, 1, 1);}

Although this method is recursive, it is not intuitive and has no advantage over the following iteration cycle in terms of efficiency.

It is easy to use iterative programs to write the Fibonacci series. The C ++ language is described as follows:

// You can also use an array to store the f (n) calculated each time. it is used for the next calculation (space for time) long fiber 3 (int n) {long x = 0, y = 1; for (int j = 1; j <n; j ++) {y = x + y; x = y-x;} return y ;}

In this case, the program efficiency is O (N), and the result is <1 s when N = 45.

Address of this article: http://www.nowamagic.net/librarys/veda/detail/601,welcome.

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.