Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help with a c++ program with explanation. Im stuck at how to begin and make the functions. Consider the following definitions: A proper
I need help with a c++ program with explanation. Im stuck at how to begin and make the functions.
Consider the following definitions:
- Aproper divisorsof a positive integer (2) is any of its divisors excluding the number itself.
- For example, the proper divisors of 10 are: 1, 2 and 5.
- Aperfect numberis apositiveinteger (2) that is equal to the sum of its properdivisors.
- For example, 6 and 28 are perfect numbers, since:
- 6=1+2+3
- 28 = 1 + 2 + 4 + 7 + 14
- Background of perfect numbers:https://en.wikipedia.org/wiki/Perfect_number
- Amicable numbersare two different positive integer (2), so related that the sum of the proper divisors of each is equal to the other number.
- For example, 220 and 284 are amicable numbers, since:
- 284 = 1 + 2 + 4 + 5 + 10 + 11 + 20 + 22 + 44 + 55 + 110
- 220 = 1 + 2 + 4 + 71 + 142
- Background of amicable numbers: https://en.wikipedia.org/wiki/Amicable_numbers
- using a function:
- void analyzeDividors(int num, int& outCountDivs, int& outSumDivs)
- The function takes as an input a positive integernum(2), and updates two output parameters with the number ofnum's proper divisors and their sum.
- For example, if this function is called withnum=12, since 1, 2, 3, 4 and 6 are 12s proper divisors, the function would update the output parameters with the numbers 5 and 16. Note:Pay attention to the running time of your function. An efficient implementation would run in (num).
- Use the function you wrote in section (a), to implement the function:
- bool isPerfect(int num)
- This function is given a positive integernum(2), and determines if it is a perfect number.
- Use the functions you implemented in sections (a) and (b), to make a program that reads from the user a positive integer M (2), and prints:
- All the perfect numbers between 2 and M.
- All pairs of amicable numbers between 2 and M (both numbers must be in the range). Note:Pay attention to the running time of your implementation.An efficient algorithm for this part would callanalyzeDividors (M)times all together.
- Your program should interact with the userexactlyas it shows in the following example:
- Enter a positive integer >= 2:2000
- The perfect numbers between 2 and your number are:
- 6 28 496
- The pairs of amicable numbers that are between 2 and your number are:
- 220 284
- 1184 1210
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started