Skip to content

Commit

Permalink
Add russian peasant and implementation in python
Browse files Browse the repository at this point in the history
  • Loading branch information
ZoranPandovski committed Jun 22, 2017
1 parent 0f8cf55 commit e4de4a2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Russian_Peasant/Python/ZoranPandovski/russian_peasant.py
@@ -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 Russian_Peasant/Python/ZoranPandovski/russian_peasant_test.py
@@ -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()
2 changes: 2 additions & 0 deletions Russian_Peasant/README.md
@@ -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)
2 changes: 2 additions & 0 deletions Russian_Peasant/tags
@@ -0,0 +1,2 @@
multiplication
arithmetic

0 comments on commit e4de4a2

Please sign in to comment.