Question

Write in C++ Programming (Spring 2020) Question 5 [15pts] Write a function to find the first...

Write in C++ Programming (Spring 2020)

Question 5 [15pts]

Write a function to find the first 45 Amicable pairs of numbers (series A259180

on the On-line Encyclopedia of Integer Sequences). A pair of numbers

x

and

y

is called amicable if the

sum of the proper divisors of either one is equal to the other. For example, 220 and 284 is a pair of

amicable numbers. The divisors or 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110. The sum of all those

numbers is equal to 284. The divisors of 284 are 1, 2, 4, 71, 142, which add up to 220. A proper divisor

is one that divides the number without a remainder.

Hint:

use auxiliary functions to break down the

complexity of the task

0 0
Add a comment Improve this question Transcribed image text
Answer #1

amicable.cpp

#include<iostream>
#include<math.h>
using namespace std;

//Function to find factorsum
long factorSum(long n)
{
   //sum is intialized to 1 because 1 is factor of every numebrs
   long sum=1;
   //Iterate from 2 to square root of n
   for(int i=2;i<=sqrt(n);i++)
   {
       //If i is a factor of n
       if(n%i==0)
       {
           //49/7 = 7 then add 7 only once
           if(n/i==i)
           {
               sum+=i;
           }
           //Add 2 factors
           else
           {
               sum+=(n/i + i);
           }
       }
   }
   //return sum
   return sum;
}

int main()
{
   long count=0,num=2;
   //Iterate until 45 Amicable pair were found
   while(count<45)
   {
       //find factorsum of num and store in variable sum1
       long sum1 = factorSum(num);
       //find factorsum of sum1 and store in variable sum2
       long sum2 = factorSum(sum1);
       //num!=sum1 becasue factorSum(28)=28 but 28 is not Amicable pair
       //If num is eqaul to sum2 then num, sum1 are Amicable pair
       if(num!=sum1 && num==sum2)
       {
           cout<<num<<" "<<sum1<<endl;
           //next num starts at sum1
           num=sum1;
           //increment the Amicable paris counts
           count++;
       }
       num++;
   }
   return 0;
}

output screenshot:

Add a comment
Know the answer?
Add Answer to:
Write in C++ Programming (Spring 2020) Question 5 [15pts] Write a function to find the first...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++, data structure An integer number’s proper divisor is any positive integer that divides the number...

    C++, data structure An integer number’s proper divisor is any positive integer that divides the number without remainder and is less than the number. Neither zero nor any negative number is a proper divisor. Write a function returns true if its second parameter is a proper divisor of its first parameter. The function’s prototype is bool properDivisor(int number, int candidate) Write a function that returns the sum of a number’s proper divisors. The function’s prototype is int properDivisorSum(int number); Use...

  • Write a program that asks the user for a lower limit and an upper limit. The...

    Write a program that asks the user for a lower limit and an upper limit. The program finds all pairs of amicable numbers where the first number of the pair is between those limits. The second number of the pair may or may not be between the limits. To do this sensibly, write a function int sumDivisors( int num ); that returns the sum of all the proper divisors of num. The main program looks at each integer N between...

  • C++ 2. (a) Write a function, printdixisors, that takes a single integer parameter and prints all...

    C++ 2. (a) Write a function, printdixisors, that takes a single integer parameter and prints all the numbers less that the parameter that are divisors of the parameter (i.e. divides it without a remainder) including 1. So printdivisors(6) will print 1,2,3. Note you may use a wrapper function or default parameters. (b) Write a function, sumdixisors, that takes a single integer parameter and returns the sum of all the divisors of the parameter (including 1). So sumdivisors(6) will return 6...

  • Question is two parts In R(Programming language). Define and write a function which takes one number...

    Question is two parts In R(Programming language). Define and write a function which takes one number as input and detects whether the number is even. Return TRUE if it is even, and FALSE otherwise. You need to first detect if the input is an integer; if not, return NA. Note 1: TRUE and FALSE are logical values, not strings! Note 2: No need to get user input from the console. Test your function using your_function_name(1), your_function_name(2), and your_function_name(1.5). Hint: What...

  • Use C programming Make sure everything works well only upload Write a program that takes an...

    Use C programming Make sure everything works well only upload Write a program that takes an integer from standard input and prints all prime numbers that are smaller than it to standard output. Recall that a prime number is a natural number greater than 1 whose only positive divisors are 1 and itself. At the start of the program, prompt the user to input a number by printing "Input a number greater than 2:\n". If the user's input is less...

  • OLA203 Exercise Due (by midnight): February 13, 2020 NOTE: Please remember that exercises are due by...

    OLA203 Exercise Due (by midnight): February 13, 2020 NOTE: Please remember that exercises are due by the stated time and cannot be late. OBJECTIVE: Practice using CodeLite and C++, especially functions, if statements, while loops, and opening/closing files. Again, this solo assignment should help you honestly assess your programming skill level and personal understanding of C++. PROBLEM BACKGROUND: An integer N (greater than or equal to 2) is prime if and only if the only integer larger than 1 that...

  • The polynomial addition C function of Program 2.6 padd is the code when the polynomial is used to...

    The polynomial addition C function of Program 2.6 padd is the code when the polynomial is used to arrange the polynomial in the two arrangement methods of the polynomial described in the text 2.4.2. For the remaining method, when the expression polynomial is arranged by a coefficient, create a polynomial addition C function padd() corresponding to Program 2.6. 66 Arrays And Structures are zero are not displayed. The term with exponent equal to zero does not shouw able since x...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT