Question

In: Computer Science

A function calledsumOfFactorsthat takes n as a parameter and then returns the sum of the proper...

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.

Solutions

Expert Solution

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.


Related Solutions

Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a method sumTo that accepts an integer parameter n and returns the sum of the...
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words: sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should print an error message and return -1 if passed a value less than 0. Include a loop. Please help for Java programming.
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a function convert_date that takes an integer as a parameter and returns three integer values...
Write a function convert_date that takes an integer as a parameter and returns three integer values representing the input converted into days, month and year (see the function docstring). Write a program named t03.py that tests the function by asking the user to enter a number and displaying the output day, month and year. Save the function in a PyDev library module named functions.py A sample run for t03.py: Enter a date in the format MMDDYYYY: 05272017 The output will...
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
Write a function called alter_sum(n)that returns the alternating sum of all the numbers from 1 to...
Write a function called alter_sum(n)that returns the alternating sum of all the numbers from 1 to n so that the first number is added, the second number is subtracted, the third number added, the fourth subtracted, and so on: 1-2+3-4+5-6+7… until you reach n. If n is 0 or less then return 0.
Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text...
Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text object. The function definition code stub is given in the editor. Perform the given operation for the 'text' object and print the results: • Filter the words whose length is greater than 15 from the complete set of 'text', and store into "large_words' variable as a list
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns...
PYTHON 3: Write a recursive function that takes a non-negative integer n as input and returns the number of 1's in the binary representation of n. Use the fact that this is equal to the number of 1's in the representation of n//2 (integer division) plus 1 if n is odd. >>>numOnes(0) 0 >>>numOnes(1) 1 >>>numOnes(14) 3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT