Use Python An abundant number is an integer that is less tha

Use Python:

An abundant number is an integer that is less than the sum of its perfect divisors (a perfect divisor is a value that divides a number evenly but is strictly less than the number itself). For example, 12 is an abundant number because its perfect divisors (1, 2, 3, 4, and 6) add up to 16. Complete the abundant() function, which takes a single positive integer value n as its argument. The function returns a Python list containing exactly the first n abundant numbers in ascending order. For example, abundant(3) would return a list with the first three abundant numbers.

Hint: You will need two loops and several helper variables to solve this problem. Your helper variables (outside the loop) should track (among other things) the number of abundant numbers seen so far as well as the integer currently being examined (which will start at 2 and increase at the end of your outer loop). The first (outer) loop should continue until you have found the required number of abundant numbers. Inside that loop, use a second loop to find and add up the perfect divisors of the number currently being examined.

Example:

------------------

Function Call Return Value
abundant(2) [12, 18]
abundant(5) [12, 18, 20, 24, 30]
abundant(10) [12, 18, 20, 24, 30, 36, 40, 42, 48, 54]

Solution

Code:

import os
import sys

def isabund(n):
divs = [i for i in range(1,n) if n%i==0]
summat = 0
for div in divs:
summat+=div
if divs and summat > n:
#print \'n is: \'+str(n) + \'sum is \'+ str(summat)
return True
else:
return False
def abundant(n):
result =[]
count=0
i=1
while count<n:
if isabund(i):
result.append(i)
count+=1
i+=1
return result
  


if __name__ == \"__main__\":
print \'Testing abundant() for 2: \' + str(abundant(2))
print \'Testing abundant() for 5: \' + str(abundant(5))
print \'Testing abundant() for 10: \' + str(abundant(10))   
  

Use Python: An abundant number is an integer that is less than the sum of its perfect divisors (a perfect divisor is a value that divides a number evenly but is

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site