Home > Backend Development > PHP Tutorial > PHP implements Russian multiplication examples, Russian multiplication examples_PHP tutorial

PHP implements Russian multiplication examples, Russian multiplication examples_PHP tutorial

WBOY
Release: 2016-07-13 10:05:28
Original
881 people have browsed it

PHP implements Russian multiplication examples, Russian multiplication examples

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:

<&#63;php
function russian($m, $n, $res = 0){
  (1 == ($n & 1)) && $res += $m;
  $m = $m << 1;
  $n = $n >> 1;
  return $n &#63; russian($m, $n, $res) : $res;
}
echo russian(7, 8);
Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/963831.htmlTechArticlePHP implementation of Russian multiplication examples, Russian multiplication examples This article describes how PHP implements Russian multiplication. Share it with everyone for your reference. The specific analysis is as follows: 1. Overview:...
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