In: Computer Science
A function calledsumOfFactorsthat takes n as a parameter and then returns the sum of the proper factors/divisors ofn; for example,the sum of factorsof 20= 10+5+4+2 = 21.Note that 1 is nota properfactor of any number.Sample command => output:(display (sumOfFactors20))=> 21Hint: Calla recursivefunction sum_helperfrom sumOfFactorsfunction as follows:(define (sumOfFactors n)(sum_helper n (-n 1)))Here sum_helperfunction takes two parameters: n, d and returns the sum of all factors of n whichare <= d; for e.g. (sum_helper 20 6) returnsall proper factors of 20 which are <=6, i.e.,it will returnsthe value of 5+4+2 = 11.
temp=set()
def sumOfFactors(n):
factors=[]
for i in range(2, n): #GETTING FACTORS OF n
if n % i == 0:
factors.append(i)
d=int(input("Enter the value of d="))
helper=sum_helper(n,d) #CALLING THE SUM_HELPER RECURSIVE FUNCTION
sec_sum=0
first_sum=0
for i in (helper):
sec_sum+=i #ADDING THE FACTORS WHICH ARE LESS THAN d
for i in factors:
first_sum+=i #ADDING THE FACTORS
return (first_sum,sec_sum)
def sum_helper(n,d):
if (d > 1):
if (n % d == 0):
global temp
temp.add(d)
sum_helper(n, d-1) #RECURSIVE FUNCTION
return temp
sumOfFactors(20) #Calling the function, Change the value inside the bracket.
--------------------------------------------------------------
SUMMARY:-
Since the programming language is not mentioned here is the
python code which is implemented as per your need.
You can change the value of n in the last line
"sumOfFactors(20)" by replacing 20.