Cow 2-year-old calf 5 years later and died algorithm

Source: Internet
Author: User

Colleagues raised a question about the cow calf algorithm, the previous cow calf algorithm is assumed that the cow is not dead, a few years, a few years later, how many head, the algorithm is somewhat different.

Farm first year there is a cow, assuming that the cow every 2 years a calf, calf quickly grow up, 2 years later can be born calf, but the cattle live until the age of 5 to return to natural death, then the nth year how many cattle.

I think the difficulty with this problem is that the cow is going to die. Naturally, a condition is set up where the cow is 5 years old and the cow dies (that is, the element disappears). Colleagues based on this idea, wrote the first version of the code

According to test instructions logic understanding, sequential logic judgment, programming code is as follows

<?PHP$fun=function($n){    $list=[1];  while($n--) {        foreach($list  as $k=$v) {            if($v%2==0)            {                $list[]=1;//give birth to a calf            }            if($v==5)            {                unset($list[$k]);//By the age of five, the bull is dead.                Continue; }            $list[$k]++;//The cow grew up one year old        }    }    return Count($list);//returns the number of cows};Echo $fun(10);Exit();

This code logic is very clear, is the basis of the model, however, we found that the number of times the increase in the memory will overflow, such as after 100 years after the request

Then he proposed an improvement program using ARRAY_POP Recycling

As follows

The code is as follows:

<?PHP$fun 1=function($n){    $list= [1];  while($n--){        foreach($list  as $k=$v){            if($v%2 = = 0){                $list[] = 1; }            if($v= = 5){                //unset ($list [$k]);                $list[$k] =Array_pop($list); Continue; }            $list[$k]++; }    }    return Count($list);};Echo $fun 1(100);Exit();

However, there is a memory leak and one more notice.

As for the death of the cows, and the regeneration, I came up with a plan by the number of cows, wondering if I could be understood.

As follows:

The code is as follows:

<?PHP$sum=get_cow_num (10);Var_dump($sum);Exit();functionGet_cow_num ($n){    $sum=1;  for($i= 1;$i<$n;$i++)    {        $sum=$i%2!=0?$sum* *:$sum-peibona ($i/2-1);//odd years, double even years minus Fibonacci numbers .    }    return $sum;}functionPeibona ($n){    if($n<=0) {return0;} if($n<=2) {return1;} returnPeibona ($n-1) +peibona ($n-2);}

After 100 years of calculation, 30 seconds is short.

I'm trying to extend my time.

Results......

10 minutes later, the program still does not stop the meaning, is really enough, it seems that recursion is not suitable, I guess the end will be due to recursion, error message that PHP can not be recursive more than 99 layers, because before the problem.

So I'm going to fix this problem by using the previously solved Fibonacci algorithm.

There are too many of these cows ...

The code is as follows:

<?PHPSet_time_limit(0);$sum=get_cow_num (100);Var_dump($sum);Exit();functionGet_cow_num ($n){    $sum=1;  for($i= 1;$i<$n;$i++)    {        $sum=$i%2!=0?$sum* *:$sum-peibona ($i/2-1);//odd years, double even years minus Fibonacci numbers .    }    return $sum;}functionPeibona ($n){    if($n<=0) {return0;} if($n<=2) {return1;} $array=Array_fill(0,$n, 0); $array[0]=0; $array[1]=1; $array[2]=1;  for($i= 3;$i<=$n;$i++)    {        $array[$i]=$array[$i-1]+$array[$i-2]; }    return $array[$n];}

Does this piece of code have a place to optimize? Of course, we found that every time the Fibonacci count is going to be a cyclic reset for calculation, why not use static caching?

The code is as follows:

<?PHPSet_time_limit(0);$sum=get_cow_num (100);Var_dump($sum);Exit();functionGet_cow_num ($n){    $sum=1;  for($i= 1;$i<$n;$i++)    {        $sum=$i%2!=0?$sum* *:$sum-peibona ($i/2-1,$n/2-1);//odd years, double even years minus Fibonacci numbers .    }    return $sum;}functionPeibona ($n,$total _num){    Static $array; if(!isset($array[$n]))    {        if($n<=0)        {            $array[$n]=0; }ElseIf($n<=2)        {            $array[$n]=1; }        Else        {            $array=Array_fill(0,$n, 0); $array[0]=0; $array[1]=1; $array[2]=1;  for($i= 3;$i<=$n;$i++)            {                $array[$i]=$array[$i-1]+$array[$i-2]; }        }    }    return $array[$n];}

This code, haha haha, the coquettish people can not understand Ah!

Later, according to colleagues summary of the formula, using even double, odd minus the Fibonacci number of formula, get Python code

This code is very coquettish and recursive, but I guess it should be quicker to use recursion.

About the Fibonacci formula of the number of Https://baike.baidu.com/item/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97/99145?fr=aladdin can refer to the Baidu Encyclopedia

Cow 2-year-old calf 5 years later and died algorithm

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.