using python
Write a def function “amc” with a positive integer “n” input
parameter. It returns the smallest amicable number greater than
“n”. Two different numbers are both amicable if the sum of the
proper divisors of each is equal to the other. Any number that's
part of such a pair is an amicable number.
Hint: You may want to create a separate function to sum proper
divisors.
def amc(n):
Return the smallest amicable number greater than positive
integer n.
Every amicable number x has a buddy y different from x, such that
the sum of the proper divisors of x equals y, and the sum of the
proper divisors of y equals x.
For example, 220 and 284 are both amicable because
1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110 is 284, and 1
+ 2 + 4 + 71 + 142 is 220
>>> amc(5)
220
>>> amc(220)
284
>>> amc(284)
1184
>>> r = amc(5000)
>>> r
5020
CODE
def sumOfDiv(x):
sum = 1
for i in range(2, x):
if x % i == 0:
sum += i
return sum
def isAmicable(a, b):
if sumOfDiv(a) == b and sumOfDiv(b) == a:
return True
else:
return False
def amc(n):
i = n + 1
while i < 10000:
j = 0
while j < 10000:
if isAmicable(i, j):
return i
j += 1
i += 1
Get Answers For Free
Most questions answered within 1 hours.