Question

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 the function properDivisor to write this function.

Two distinct, integer numbers, x and y are amicable numbers if the sum of x’s proper divisors equals y and the sum of y’s proper divisors equals x.

From Wikipedia

“The smallest pair of amicable numbers is (220, 284). They are amicable because the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110, of which the sum is 284; and the proper divisors of 284 are 1, 2, 4, 71 and 142, of which the sum is 220.”

Write a function, amicablePair, that returns true if its two integer parameters are amicable and false otherwise. The function’s protoype is

bool amicablePair(int a, int b);

Write a program that generates pairs of random numbers until the pair of random numbers are amicable numbers.

Write the pairs of random numbers to a text file.

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

Thanks for the question.


Here is the completed code for this problem. Let me know if you have any doubts or if you need anything to change.


Thank You !!


==============================================================================================

#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;

bool properDivisor(int number, int candidate){
   return candidate>0 && number%candidate==0;
}

int properDivisorSum(int number){
  
   int sum=0;
   for(int i=1;i<number;i++){
       if(properDivisor(number,i))
       sum+=i;
   }
   return sum;
}

bool amicablePair(int a, int b){
  
   int properDivisorSumA = properDivisorSum(a);
   int properDivisorSumB = properDivisorSum(b);
  
   return properDivisorSumA==b && properDivisorSumB==a;

}

int main(){
  
   const char * filename = "F:\\not__amicable.txt";
   ofstream outfile(filename);
  
  
   if(outfile.good()){
      
           int a = 220+rand()%2;  
           int b = 284+rand()%2;  

       while(true){
          
           if(!amicablePair(a,b)){
               outfile<<a<<", "<<b<<endl;
           }else{
               outfile<<"First Amicable Pair: "<<a<<", "<<b<<endl;
               cout<<"File updated successfully."<<endl;
               break;
           }
           a = rand()%500;  
           b = rand()%500;
          
       }
   outfile.close();
   }
  
}

Add a comment
Know the answer?
Add Answer to:
C++, data structure An integer number’s proper divisor is any positive integer that divides the number...
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
  • 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,...

  • For C++: A perfect number has these properties 1) positive integer 2) the sum of its...

    For C++: A perfect number has these properties 1) positive integer 2) the sum of its proper divisors is the number itself 6 and 28 are perfect numbers Write two functions that return true if its value parameter is a perfect number and false if its value parameter is not a perfect numbers. The two function look like bool isPerfect(int) and void isPerfect(int, bool&) If you want more perfect number you find them on the Internet.

  • 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...

  • A perfect number is a positive integer that is equal to the sum of its (proper)...

    A perfect number is a positive integer that is equal to the sum of its (proper) positive divisors, including 1 but excluding itself. A divisor of a number is one which divides the number evenly (i.e., without a remainder). For example, consider number 6. Its divisors are 1, 2, 3, and 6. Since we do not include number itself, we only have 1, 2, and 3. Because the sum of these divisors of 6 is 6, i.e., 1 + 2...

  • A perfect number is a positive integer that equals the sum of all of its divisors...

    A perfect number is a positive integer that equals the sum of all of its divisors (including the divisor 1 but excluding the number itself). For example 6, 28 and 496 are perfect numbers because 6=1+2+3 28 1 + 2 + 4 + 7 + 14 496 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248 Write a program to read a positive integer value, N, and find the smallest perfect number...

  • 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...

  • Write a function that takes a list of integer number and returns the greatest common divisor...

    Write a function that takes a list of integer number and returns the greatest common divisor of all numbers in the list. WRITE THE CODE IN PYTHON

  • c++ Write a rational number class. A rational number is a number that can be written...

    c++ Write a rational number class. A rational number is a number that can be written as p/q where p and q are integers. The division is not carried out, only indicated. Thus you should represent rational numbers by two int values, numerator and denominator. Constructors must be present to create objects with any legal values. You should provide constructors to make objects out of pairs of int values; that is, a constructor with two int parameters. Since very int...

  • A prime number is a positive integer whose positive integer divisors are 1 and the number....

    A prime number is a positive integer whose positive integer divisors are 1 and the number. E.g. 17 is a prime number -23 is a not prime number 28 is not a prime number -45 is not a prime number The only even prime number is 2 Write a boolean function, isPrime that return true if its integer parameter is a prime and false if its integer parameter is not a prime number. A simple prime number algorithm is if...

  • Please Write the task in c++ Task A perfect number is an integer that is equal...

    Please Write the task in c++ Task A perfect number is an integer that is equal to the sum of its divisors (where 1 is considered a divisor). For example, 6 is perfect because its divisors are 1, 2, and 3, and 1 + 2 + 3 is 6. Similarly, 28 is perfect because it equals 1 + 2 + 4 + 7 + 14. A quite good number is an integer whose badness—the size of the difference between the...

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