我必须计算至少有多少个连续的自然数(即正整数,例如1、2、3,...),我们必须乘以至少输入乘积(因此,数字的乘积必须大于或等于输入的数字)。使用循环进行计算。程序应在语句中列出答案。如果输入33,答案是:“要获得至少33,我们需要将前5个自然数相乘。”
答案 0 :(得分:0)
尝试:
number_calc = 33
def calculate(number):
last_number = 1
prod = 1
if number == 1:
pass
else:
while prod <= number :
last_number = last_number + 1
prod = prod*last_number
return last_number
answer = calculate(number_calc)
print("To get at least {}, we need to multiply the first {} natural numbers.".format(number_calc,answer))
但是您应该听这些注释并自己尝试一下,这是一个非常基本的代码示例。
答案 1 :(得分:0)
尝试以下代码:
x = int(input("Enter a Number"))
count = 1
j = 1
for i in range(1,x):
count = count * i
if count >= x: # Count till your product is greater than or equal to entered number
j = i
break # Break here to avoid anymore loop
print ("To get at least {}, we need to multiply the first {} natural numbers.".format(x,j))