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 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 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 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 named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "w" replaced with the character "v" My code: function replacement(word){ var str=word; var n=str.replace("w","v"); return n; } Syntax Error: function replacement incorrect on input Not sure how to fix? Can't use a loop for answer
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...
a. Write a function sumDigits that takes a positive integer value and returns the total sum...
a. Write a function sumDigits that takes a positive integer value and returns the total sum of the digits in the integers from 1 to that number inclusive. b. Write a program to input an integer n and call the above function in part a if n is positive, else give ‘Value must be Positive’ message. Sample Runs: Enter a positive integer: 1000000 The sum of the digits in the number from 1 to 1000000 is 27000001 Enter a positive...
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
Write a Scheme function that takes a positive integer n and returns the list of all...
Write a Scheme function that takes a positive integer n and returns the list of all first n positive integers in decreasing order: (decreasing-numbers 10) (10 9 8 7 6 5 4 3 2 1) Please explain every step
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT