Home > Backend Development > PHP Tutorial > PHP implementation of Russian multiplication example_PHP tutorial

PHP implementation of Russian multiplication example_PHP tutorial

WBOY
Release: 2016-07-13 10:05:09
Original
1037 people have browsed it

Example of implementing Russian multiplication in PHP

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:

?

1

2

3

4

5

6

7

8

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);

1 2

3

4 5

67 8
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. http://www.bkjia.com/PHPjc/963967.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963967.htmlTechArticleExample of implementing Russian multiplication in PHP. This article mainly introduces the implementation of Russian multiplication in PHP. The example analyzes the principles and principles of Russian multiplication. Code implementation skills have certain reference value and are needed...
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