Algorithm Analysis (1) Fibonacci number

Source: Internet
Author: User
Note: Fibonacci number
Description: Fibonacci ' soriginal question:

1. Given a pair of rabbits, a male, a female.

2. Rabbits at one months old, can be mated, the Mother Rabbit in the next one months, can give birth to another pair of rabbits.

3. Suppose our rabbits never die.

4. Assume that each female rabbit is always reborn with a rabbit (one is male, one is female) question: how many pairs of rabbits a year later.
Analytical:

1. In the beginning: (1 pair)
2. End of Month 1: (1 pair) Rabbitsare ready to mate.
3. End of Month 2: (2 pairs) A Newpair of rabbits is born.
4. End of Month 3: (3 pairs) A Newpair and both old pairs.
5. End of Month 4: (5 pairs)
... 6. End of Month 5: (8 pairs)
...
..... 7.After months, there'll be233 pairs rabbits!


{0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...}


Idea 1:

ALGOTITHM1 fib (N)

If n = 0 then return 0

if n = 1 then return 1

Return (FIB (n-1) +fib (n-2))

Efficiency: t (n) = t (n−1) + t (n−2) + 3≈1.6 n-th square t (+) >= 2 139-square (291 seconds = 7.85x1019years)



Idea 2: Implement ALGORITHM2:FIB with an array (n)

Initially we create an array a[0:n]

A[0]←0, a[1]←1

For i= 2 to n do

a[i]= a[i−1] + a[i−2]

Return (A[n])

Similar issues:

/*<span style= "FONT-SIZE:14PX;" > title: There are a pair of rabbits, from the 3rd month after birth a pair of rabbits each month, the small rabbit to the third month after the birth of a pair of rabbits each month, if the rabbit is not dead, ask each month the total number of rabbits. </span> * Analysis: First month 1 to second month 1 to third month 2 to 2nd pair Growth 1 month fourth month 3 to 3rd to growth 1 months 2nd to Growth 2 month fifth month 5 to 5th to Growth 1 month 4th to growth 1 Month 3rd to grow 2 months sixth month 8 to 8th, 7, 6 to Growth 1 months 5th, 4 to grow 2 months seventh Month 13 pair * Difficulty: Find the Law 1. Why the third month later. 
The number of rabbits per month is the number of rabbits in the first two months.  
    */#include <stdio.h> int main () {int m[23];  
    int i;  
    M[0]=m[1]=1;  
        for (i=0; i<24; i++) {if (i==0 | | i==1) {printf ("%d months: 1 pair \ n", i+1);  
            } else {m[i]=m[i-1]+m[i-2];  
              
        printf ("%d months:%d to \ n", I+1,m[i]);  
}} return 0; }/* Version two #include <stdio.h> int fun (int n) {for (n = 1;n<12;n++) {if (N==1 | | n  
    	==2) return 1;  
	else return Fun (n-1) +fun (n-2);  
    }} int main () {int mouth;  
    printf ("You need to query the number of rabbits in the first few months"); scanf"%d", &mouth);  
    Fun (mouth);  
    printf ("Number of rabbits%d months:%d to \ n", mouth,fun (mouth));  
return 0;  




 } */



































































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.