Home > Article > Backend Development > PHP implementation of monkey picking peach algorithm code sharing
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 '<hr/>';
Method 2
##
$a=1; for($i=10;$i>1;$i--){ $a=($a+1)*2; } echo '一共摘了',$a,'个桃子'; echo '<hr/>';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!