📌  相关文章
📜  俄罗斯农民(使用按位运算运算符将两个数字相乘)

📅  最后修改于: 2021-04-27 23:08:03             🧑  作者: Mango

给定两个整数,编写一个不使用乘法运算符即可将它们相乘的函数。
还有许多其他方法可以将两个数字相乘(例如,请参见此内容)。一种有趣的方法是俄罗斯农民算法。想法是将第一个数字加倍,然后将第二个数字减半,直到第二个数字不等于1。在此过程中,每当第二个数字变为奇数时,我们将第一个数字加到结果中(结果初始化为0)
以下是简单的算法。

Let the two given numbers be 'a' and 'b'
1) Initialize result 'res' as 0.
2) Do following while 'b' is greater than 0
   a) If 'b' is odd, add 'a' to 'res'
   b) Double 'a' and halve 'b'
3) Return 'res'. 
C++
#include 
using namespace std;
 
// A method to multiply two numbers using Russian Peasant method
unsigned int russianPeasant(unsigned int a, unsigned int b)
{
    int res = 0; // initialize result
 
    // While second number doesn't become 1
    while (b > 0)
    {
        // If second number becomes odd, add the first number to result
        if (b & 1)
            res = res + a;
 
        // Double the first number and halve the second number
        a = a << 1;
        b = b >> 1;
    }
    return res;
}
 
// Driver program to test above function
int main()
{
    cout << russianPeasant(18, 1) << endl;
    cout << russianPeasant(20, 12) << endl;
    return 0;
}


Java
// Java program for Russian Peasant Multiplication
import java.io.*;
 
class GFG
{
    // Function to multiply two
    // numbers using Russian Peasant method
    static int russianPeasant(int a, int b)
    {
        // initialize result
        int res = 0; 
  
        // While second number doesn't become 1
        while (b > 0)
        {
             // If second number becomes odd,
             // add the first number to result
             if ((b & 1) != 0)
                 res = res + a;
  
            // Double the first number
            // and halve the second number
            a = a << 1;
            b = b >> 1;
        }
        return res;
    }
     
    // driver program
    public static void main (String[] args)
    {
        System.out.println(russianPeasant(18, 1));
        System.out.println(russianPeasant(20, 12));
    }
}
 
// Contributed by Pramod Kumar


Python 3
# A method to multiply two numbers
# using Russian Peasant method
 
# Function to multiply two numbers
# using Russian Peasant method
def russianPeasant(a, b):
 
    res = 0 # initialize result
 
    # While second number doesn't
    # become 1
    while (b > 0):
     
        # If second number becomes
        # odd, add the first number
        # to result
        if (b & 1):
            res = res + a
 
        # Double the first number
        # and halve the second
        # number
        a = a << 1
        b = b >> 1
     
    return res
 
# Driver program to test
# above function
print(russianPeasant(18, 1))
print(russianPeasant(20, 12))
# This code is contributed by
# Smitha Dinesh Semwal


C#
// C# program for Russian Peasant Multiplication
using System;
 
class GFG {
     
    // Function to multiply two
    // numbers using Russian Peasant method
    static int russianPeasant(int a, int b)
    {
        // initialize result
        int res = 0;
 
        // While second number doesn't become 1
        while (b > 0) {
             
            // If second number becomes odd,
            // add the first number to result
            if ((b & 1) != 0)
                res = res + a;
 
            // Double the first number
            // and halve the second number
            a = a << 1;
            b = b >> 1;
        }
        return res;
    }
 
    // driver program
    public static void Main()
    {
        Console.WriteLine(russianPeasant(18, 1));
        Console.WriteLine(russianPeasant(20, 12));
    }
}
 
// This code is contributed by Sam007.


PHP
 0)
    {
         
        // If second number becomes odd,
        // add the first number to result
        if ($b & 1)
            $res = $res + $a;
 
        // Double the first number and
        // halve the second number
        $a = $a << 1;
        $b = $b >> 1;
    }
    return $res;
}
 
    // Driver Code
    echo russianPeasant(18, 1), "\n";
    echo russianPeasant(20, 12), "\n";
     
// This code is contributed by Ajit
?>


Javascript


输出:

18
240