Question

Assignment: Euclid’s algorithm for finding the greatest common divisor (gdc) of two numbers is fairly simple,...

Assignment: Euclid’s algorithm for finding the greatest common divisor (gdc) of two numbers is fairly simple, but there are some possible problems that we will guard against.

The algorithm: given two numbers, n1 and n2:

  1. Divide n1 by n2 and let r be the remainder.
  2. If the remainder r is 0, the algorithm is finished and the answer is n2. (If the remainder is 1, the numbers are mutually prime-see below.)
  3. Set n1 to the value of n2, set n2 to the value of remainder r and go back to step 1.

There are some things to watch out for:

  • For the purposes of this exercise use integer values for n1 and n2.
  • Entering 0 for one of the values is bad. It should work for the other value, but you have to figure out which is OK and which is bad.
    • Catch this problem as it happens and make the user enter another value until they enter an acceptable one.
    • Give an appropriate error message if this happens.
  • The user should be prompted to enter values for n1 and n2.
    • For our purposes entering either n1 or n2 less than zero is an error:
      • Catch these errors and make the user enter another value until they enter an acceptable one for the value in error.
      • Give an appropriate error message if this happens.
      • Hint: the program code for catching errors in the entry of n1 and n2 is different.
  • If the remainder is 1, the two numbers are mutually prime:
    • This means that they have no common divisors except 1.
    • In this case print a message informing the user of that circumstance
  • If the two numbers have a greatest common divisor other than 1, print the two numbers and their gcd in an informative message.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

************************************CODE*********************************

#include <bits/stdc++.h>
using namespace std;
int gcd_n1_n2(int a, int b)
{
if (b == 0)
return a;
return gcd_n1_n2(b, a % b);
  
}
int main()
{
int n1,n2;
cout<<"Enter the first number\n";
cin>>n1;
while(n1<=0)
{
cout<<"Enter the first number again as n1 <= 0.\n";
cin>>n1;
}
  
cout<<"Enter the second number\n";
cin>>n2;
while(n2 <= 0)
{
cout<<"Enter the second number again as n2 <= 0.\n";
cin>>n2;
}
  
int gcd = gcd_n1_n2(n1,n2);
if(gcd == 1)
cout<<"Two numbers "<<n1 <<" and "<<n2<<" are mutually prime.";
else
cout<<"GCD of two numbers "<<n1 <<" and "<<n2<<" is: "<<gcd;
return 0;
}

CODE SCREENSHOT:

OUTPUT SCREENSHOT:

Please upvote the answer if you find it useful and comment down below for any queries.

Add a comment
Know the answer?
Add Answer to:
Assignment: Euclid’s algorithm for finding the greatest common divisor (gdc) of two numbers is fairly simple,...
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
  • Cryptography Computer Security Greatest Common Divisor Assignment Instructions In software, implement the Euclidean algorithm to find...

    Cryptography Computer Security Greatest Common Divisor Assignment Instructions In software, implement the Euclidean algorithm to find the greatest common divisor of any two positive integers. It should implement the pseudocode provided in the text. It should allow the user to enter two integers.   Your program should output the intermediate values of q, r1, r2 for each step and should return the greatest common divisor. Challenge component: Allow the user's input to be zero as well as the positive integers. Provide...

  • 1. (10 points) GCD Algorithm The greatest common divisor of two integers a and b where...

    1. (10 points) GCD Algorithm The greatest common divisor of two integers a and b where a 2 b is equal to the greatest common divisor of b and (a mod b). Write a program that implements this algorithm to find the GCD of two integers. Assume that both integers are positive. Follow this algorithm: 1. Call the two integers large and small. 2. If small is equal to 0: stop: large is the GCD. 3. Else, divide large by...

  • Implement in Scheme Euclid’s algorithm computing the greatest common divisor of two integers as a tail...

    Implement in Scheme Euclid’s algorithm computing the greatest common divisor of two integers as a tail recursive function. What is the class of arithmetic functions that can be written in tail recursive way? Hint: Generalize out of examples you are familiar with.

  • Use the Division Algorithm to find the greatest common divisor of each pair of numbers below...

    Use the Division Algorithm to find the greatest common divisor of each pair of numbers below and determine whether each pair is rela- tively prime or not. Then reverse the process and write the gcd as a sum of multiples of the original pair. a. 12 and 15 b. 36 and 72 c. 27 and 10 d. 35 and 12

  • Write Java program to take two numbers as input and find the greatest common divisor among...

    Write Java program to take two numbers as input and find the greatest common divisor among the two numbers. You will need to implement a method takes two parameters as input. Sample Input and Output: Enter first integer: 80 Enter second integer: 160 The greatest common divisor for 80 and 160 is 80 Enter first integer: 60 Enter second integer: 185 The greatest common divisor for 60 and 185 is 5

  • Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers...

    Using SPIM, write and test a program that finds the Greatest Common Divisor of two integers using a recursive function that implements Euclid's GCD algorithm as described below. Your program should greet the user "Euclid's GCD algorithm", prompt the user to input two integers, and then output the result "Euclid's Greatest Common Divisor Algorithm" GCD(M,N) = M                      (if N is 0) GCD(M,N) = GCD(N, M % N)   (if N > 0) you may assume that inputs are non-negative name your assembly...

  • Write a java recursive program to calculate the greatest common divisor of two integer numbers. The...

    Write a java recursive program to calculate the greatest common divisor of two integer numbers. The program asks user to type two numbers a and b(suppose a>b). If b is 0, return a; else recursively call the method with two smaller parameters, one is b, the second is a mod b.

  • I want the code in C++ The greatest common divisor (GCD) of two integers is the...

    I want the code in C++ The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the numbers. Write a function called GCD that has a void return type, and accepts 3 parameters (first two by value, third by reference). The function should find the greatest common divisor of the first two numbers, and have the result as its OUTGOING value. Write a main function that asks the users for two integers, and...

  • Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g...

    Use R language to program Problem 1: Greatest Common Divisor (GCD) Please write two functions, g edi ) and gcdr , which both take two integers a, b and calculates their greatest common divisor (GCD) using the Euclidean algorithm gcdi () should do so using iteration while gcdr () should use recursion. Then write a third function, gcd(), which takes two integers a, band an optional third argument nethod which takes a charater string containing either "iterative" or "recursive", with...

  • PYTHON In mathematics, the Greatest Common Divisor (GCD) of two integers is the largest positive integer...

    PYTHON In mathematics, the Greatest Common Divisor (GCD) of two integers is the largest positive integer that divides the two numbers without a remainder. For example, the GCD of 8 and 12 is 4. Steps to calculate the GCD of two positive integers a,b using the Binary method is given below: Input: a, b integers If a<=0 or b<=0, then Return 0 Else, d = 0 while a and b are both even do a = a/2 b = b/2...

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