Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add russian peasant and implementation in python
- Loading branch information
1 parent
0f8cf55
commit e4de4a2
Showing
4 changed files
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
def russian_peasant(a,b): | ||
x = a | ||
y = b | ||
z = 0 | ||
while x > 0: | ||
if x % 2 == 1: | ||
z = z + y | ||
y = y << 1 | ||
x = x >> 1 | ||
|
||
return z |
9 changes: 9 additions & 0 deletions
9
Russian_Peasant/Python/ZoranPandovski/russian_peasant_test.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import unittest | ||
from russian_peasant import russian_peasant as rp | ||
|
||
class RussianPeasantTest(unittest.TestCase): | ||
def test_correct(self): | ||
self.assertEquals(rp(5,15), 75) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
### Russian Peasant | ||
Given two integers, write a function to multiply them without using multiplication operator. The idea is to double the first number and halve the second number repeatedly till the second number doesn’t become 1. In the process, whenever the second number become odd, we add the first number to result (result is initialized as 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
multiplication | ||
arithmetic |