Skip to content

Commit 524a137

Browse files
Merge pull request matthewsamuel95#817 from linj2015/russian-peasants-algorithm
Create C++ solution to Russian Peasants Algorithm
2 parents 3ecdb3e + c70703e commit 524a137

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int multiply(int a, int b)
5+
{
6+
int prod = 0;
7+
while (b > 0)
8+
{
9+
if (b % 2 != 0)
10+
{
11+
prod += a;
12+
}
13+
a = a << 1; // multiply by 2
14+
b = b >> 1; // divide by 2
15+
}
16+
17+
return prod;
18+
}
19+
20+
int main()
21+
{
22+
int a, b;
23+
cin >> a >> b;
24+
cout << multiply(a, b) << endl;
25+
26+
return 0;
27+
}

0 commit comments

Comments
 (0)