Home > Article > Backend Development > PHP implements Russian multiplication examples, Russian multiplication examples_PHP tutorial
The examples in this article describe how PHP implements Russian multiplication. Share it with everyone for your reference. The specific analysis is as follows:
1. Overview:
Russian multiplication is an algorithm for calculating the multiplication of two numbers.
Examples are as follows:
Calculate 35*72
Process
35 72
17 144
8 288
4 576
2 1152
1 2304
From top to bottom, for each row, if the number on the left is an odd number, take out the number on the right and add it up.
72+144+2304=2520
The accumulated result 2520 is the product.
2. Implementation code:
<?php function russian($m, $n, $res = 0){ (1 == ($n & 1)) && $res += $m; $m = $m << 1; $n = $n >> 1; return $n ? russian($m, $n, $res) : $res; } echo russian(7, 8);
I hope this article will be helpful to everyone’s PHP programming design.