This article mainly introduces the implementation of Russian multiplication in PHP. The example analyzes the principle and code implementation skills of Russian multiplication. It has certain reference value and is needed. Friends can refer to it
The example in this article describes how to implement Russian multiplication in PHP. 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:
?
3 4 5
|
function russian($m, $n, $res = 0){
(1 == ($n & 1)) && $res += $m;
$m = $m << 1;
$n = $n >> 1; |