俄罗斯农民增殖Python 3.3

时间:2013-02-01 01:07:21

标签: python if-statement python-3.x while-loop

我需要帮助python 3.3中的程序,它应该做俄罗斯农民繁殖/古埃及繁殖。赋值说,“如果”A“和”B“是要乘以的两个​​整数,我们反复将”A“乘以2并将”B“除以2,直到”B“不能再除以且不为零(在每个乘法“A”和除以“B”的过程中,如果“B”的值是一个奇数,你可以将“A”值添加到总数中。最后,总和所有“A”值(当“B”为奇数时)应等于原“A”和“B”输入的乘积。简而言之,总结“B”为奇数的所有“A”值它将与“A”和“B”的乘积相等(或接近)。

编辑

我可能已经说错了一些问题。

以下是一个例子:

如果“A”为34,“B”为19,则将“A”乘以2并将“B”除以每行两行。

“A”“B”

(34)(19)(“B”为奇数,将“A”加到总数中)

(68)(9)(“B”为奇数,将“A”加到总数中)

(136)(4)(“B”为偶数,忽略“A”值)

(272)(2)(“B”为偶数,忽略“A”值)

(544)(1)(“B”为奇数,将“A”加到总数中)

当你对“B”为奇数的“A”的所有值求和时,得到(34 + 68 + 544 = 646), 等于只是乘以“A”和“B”,(34 * 19 = 646)。

我遇到麻烦的部分是每当“B”为奇数时就将“A”加到总数中。

这是我到目前为止所拥有的,

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))

我是python和编程的新手,所以任何帮助和/或解释为什么它的错误都会受到高度赞赏。

4 个答案:

答案 0 :(得分:3)

我不熟悉您尝试实现的算法,但我对您的代码进行了一些修改。

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))

答案 1 :(得分:3)

您需要先添加x才能回答,然后再更新x

这是正确的代码

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))

答案 2 :(得分:0)

旁注。坦率地说,直到现在我才知道算法。古代埃及人或古老的俄罗斯使用它越令我惊讶。 (实际上,我倾向于认为俄罗斯血统似乎更可能,因为斯拉夫国家似乎与旧的伊特鲁里亚斯直接相关)。

古老的起源令我惊讶,因为它实际上是你在基础学校学到的简单的手工制作的乘法。唯一的区别是数字首先转换为二进制表示。而不是机器导向,不是吗? :)

对于问题中的数字,十进制表示中的34等于二进制中的100010,十进制中的19是二进制中的10011。现在简单的基础学校在纸上增加:

    100010
x    10011
------------
    100010   i.e. 100010 times 1
   100010        1000100 times 1
  000000        10001000 times 0
 000000        100010000 times 0
100010        1000100000 times 1
------------                   ^ binary digits of the multiplier
1010000110                       (reminder of division by 2)
                       ^ adding the zero means multiplying by 2
i.e. sum only when 1 is the reminder of division

似乎设计metod的人(在古代)知道二进制数是什么。

答案 3 :(得分:0)

最后一次添加x是y等于1.如果你保持减半,直到数字 达到0,将需要大量的迭代(逻辑上,它将需要 永远)。

想想2x2。如果你将x加倍为4,并将y减半,x就是你的答案。

换句话说,将y视为how many of x I need to yield answer。由于你不能相乘,只能加/双/减半,你可以选择 - 你可以等到y为2,然后加上x的加倍值,或者你可以等到y为1并简单地加上x的值。后者更简单,更清晰,而且都是。

我认为在这种情况下使用while True循环更清楚:

def mult2(x, y):
    answer = 0
    while True:
        if y % 2:
            answer += x
        if y < 1:
            break
        x *= 2
        y //= 2
    print("the product is", answer)

递归调用函数:

def mult2(x, y):
    """aka 'Russian peasant method'; allowed: addition, halving, doubling."""
    if y==1:
        return x
    else:
        add = x if y%2 else 0
        return mult2(x*2, y//2) + add