Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Russian Peasant Multiplication Python 3.3

I need help with a program in python 3.3 that is supposed to do Russian peasant multiplication/ancient Egyptian multiplication. The assignment says," If "A" and "B" are the two integers to be multiplied, we repeatedly multiply "A" by 2 and divide "B" by 2, until "B" cannot divide any more and is not zero (integer division). During each set of multiplying "A" and dividing "B", if the value of "B" is an odd number, you add whatever the "A" value is to a total. At the end, the sum of all the "A" values (when "B" is odd) should be equal to the product of the original "A" and "B" inputs. In short, sum up all the "A" values for which "B" is odd and it will be equal (or close) to the product of "A" and "B".

edit

I may have phrased some of the question wrong.

Here is an example:

If "A" is 34, and "B" is 19, multiplying "A" by two and dividing "B" by two each line.

"A" "B"

(34) (19) ("B" is odd, add "A" to total)

(68) (9) ("B" is odd, add "A" to total)

(136) (4) ("B" is even, ignore "A" value)

(272) (2) ("B" is even, ignore "A" value)

(544) (1) ("B" is odd, add "A" to total)

When you sum all the values of "A" for which "B" is odd, you get (34 + 68 + 544 = 646), which is equal to just multiplying "A" and "B", (34 * 19 = 646).

The part I'm having trouble with is adding "A" to a total whenever "B" is an odd number.

This is what I have so far,

x = int(input("What is the first number? "))
y = int(input("What is the second number? "))
answer = 0

while y != 0:
    if (y%2 != 0):
        x*2
        y//2
        answer == answer + x
    if (y%2 == 0):
        x*2
        y//2
print("the product is",(answer))

I'm very new to python and programming, so any help and/or explanations of why its wrong would be greatly appreciated.

like image 223
the76finals Avatar asked Feb 01 '13 01:02

the76finals


People also ask

Does Russian peasant multiplication work?

The Russian peasant multiplication method works because it converts the problem into binary (base 2) multiplication, rather than base 10.

What is the complexity of Russian peasant multiplication?

The time complexity of the piece of code you supplied is, of course, O(1) , because there is an upper bound on how long it can take and will never exceed that upper bound on any inputs.

Why is it called Russian peasant multiplication?

In 19th century it was rediscovered in Russia where it survived in daily usage and it was mostly used by uneducated peasants, and from that reason it is also called Russian (peasant) multiplication.


2 Answers

you need to add x to answer first, then update x

here is the correct code

x = int(input("What is the first number? "))
y = int(input("What is the second number? "))
answer = 0

while y != 0:
   if (y%2 != 0):
      answer=answer+x
      x=x*2
      y=y//2
   if (y%2 == 0):
      x=x*2
      y=y//2

print("the product is",(answer))
like image 158
Emmet B Avatar answered Oct 24 '22 09:10

Emmet B


I'm not familiar with the algorithm you are trying to implement, but I have made a few modifications to your code.

x = int(input("What is the first number? "))
y = int(input("What is the second number? "))
answer = 0

# != 0 is redundant: y is True if it is not 0
while y:
    # no need to have parentheses here
    if y % 2:
        # this needs to be an assignment, not a check for equality
        answer += x  # shorthand for answer = answer + x
    # These happen every time, so does not need to be inside the if
    # these also need to be an assignment, not just an expression
    x *= 2
    y /= 2
# total was never defined
print("the product is", (answer))
like image 3
Lenna Avatar answered Oct 24 '22 07:10

Lenna