PHP implementation of monkey picking peach algorithm code sharing

小云云
Release: 2023-03-20 09:52:01
Original
4893 people have browsed it

A monkey picked a bunch of peaches and ate half of them immediately, but the peaches were so delicious that he ate one more. The next day, he ate half of the remaining peaches from the first day and ate more. It picked one peach, and by the morning of the tenth day it had only one peach left. Ask it how many peaches it had picked in total? This article teaches you how to use php code to get the answer.

Method 1


function taozi($i){
 if($i==10)
 {
   return 1;
  }

 $i=(taozi($i+1)+1)*2;
 return $i;
}
echo "一共桃子有",taozi(1);
echo &#39;<hr/>&#39;;
Copy after login

Method 2


##

$a=1;
for($i=10;$i>1;$i--){
$a=($a+1)*2;
}

echo &#39;一共摘了&#39;,$a,&#39;个桃子&#39;;
echo &#39;<hr/>&#39;;
Copy after login
Related recommendations:

PHP ordered list binary search (half search) algorithm sharing

Detailed explanation of simulation curve algorithm based on PHP

PHP calculation of cosine Similarity algorithm example


#

The above is the detailed content of PHP implementation of monkey picking peach algorithm code sharing. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template