##: Multiply two numbers together. ##: Requirements: multiply by 2, divide by 2, and Add numbers ##: AKA = Mediation and Duplation Method # 17 in base 2: 10001 = 17 10001 # >> 1 << 1 # 1000 = 8 100010 = 34 import time def russian1(a,b): x = a; y = b ## Semicolon -> Compound Statement z = 0 ## Acumulator while x > 0: ## While Loop Begins if x % 2 == 1: z = z + y ## Modulo operator y = y << 1 ## Shift Binary over to left x = x >> 1 ## Shift Binary over to right return z ## Return Z def russian2(a,b): if a>b: a1 = a; a = b; b = a1 z = [] total = 0 while a > 0: a = a/2 b = b*2 z.append([a,b]) for element in z: if element[0] % 2 == 1: total += element[1] return total def test_russian(): t1 = time.time() assert russian1(357,16) == 357*16 print("Russian1 took %f to run" % (time.time()-t1)) t2 = time.time() assert russian2(357,16) == 357*16 print("Russian2 took %f to run" % (time.time()-t2)) assert russian1(12,201) == 12*201 assert russian2(12,201) == 12*201 test_russian()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question