当用户输入数字时,我尝试确定该数字是否充足。但我很困惑为什么它不起作用。自从我刚开始以来,不太擅长编码。任何帮助,将不胜感激。这是我到目前为止所拥有的:
def is_abundant():
n = raw_input("Enter a number: ")
max_divisor = int(n / 2) + 1
sum = 0
for x in range(1, max_divisor):
if n % x == 0:
sum += x
print ("your number isn't Abundant")
is_abundant()
当我输入它时,它没有给我任何东西。请帮忙!
最佳答案
刚刚查了一下充裕数的定义:
In number theory, an abundant number or excessive number is a number for which the sum of its proper divisors is greater than the number itself. The integer 12 is the first abundant number. Its proper divisors are 1, 2, 3, 4 and 6 for a total of 16. — Wikipedia
想出了这个:
def isAbundant(n):
factors = filter(lambda j: n % j == 0, range(1, n/2 + 1));
return sum(factors) > n;
或者同样,如果你喜欢俏皮话,这个:
isAbdn = lambda n: sum(filter(lambda j: n % j == 0, range(1, n/2 +1))) > n;
现在,如果您坚持使用raw_input
:
def isInputAbundant():
n = int(raw_input("Enter a number: "));
if isAbundant(n): print "YEAH! It's abundant.";
else: print "Sorry! It's not abundant.";
希望这有帮助。
关于python - 当用户输入数字时确定丰富数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33603012/