Question

can somebody help me with this exercise 5 Euclidean algorithm The largest common divisor (gcd) of...


can somebody help me with this exercise

5 Euclidean algorithm

The largest common divisor (gcd) of two positive integers p and q can be given by the
Euclid's algorithm explained in the lecture will be determined.
· Write a function gcdIterative that uses the largest common divisor of p and q
Calculates loop structure and returns. Use the pseudocode given in the lecture
as a starting point and implement it as directly as possible into a C ++ program. Use as a variable name
in your program the corresponding designations from the pseudocode.
· Write a function gcdRecursive that recursively uses the largest common divisor of p and q
calculates and returns the Euclidean algorithm. Replace the while loop with the iterative one
Variant by recursive function calls and the assignments from the iterative variant by corresponding ones
Arguments for the function parameters of the recursive function.
· Test your functions by reading values ​​for p and q and their associated largest common
Output divider.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Output:

/*------------------------------------------------------------code-------------------------------------*/

#include<iostream>
using namespace std;

int gcdIterative(int p, int q)
{
while (p != q)
   {
       if (p > q) //If p is greater than q
       p = p - q;
       else //If q is greater than p
       q = q - p;
   }
   return p;
}

int gcdIterativeEfficient(int p, int q)
{
int rem;
while(q > 0)
{
rem = p%q;
p = q;
q = rem;
}
return p;
}

int gcdRecursive(int p, int q)
{
if(p == 0)
return q;
if (q == 0)
return p;
//Base case
if (p == q)
return p;


if (p > q) //If p is greater than q
return gcdRecursive(p-q, q);
else //If q is greater than p
return gcdRecursive(p, q-p);

}

int gcdRecursiveEfficient(int p, int q)
{
if (q == 0)
return p;
return gcdRecursiveEfficient(q, p % q);
}


int main()
{
int num1,num2;
cout<<"Enter Number1: ";
cin>>num1;
cout<<"Enter Number2: ";
cin>>num2;
cout<<"Result from Iterative Method: "<<gcdIterative(num1, num2)<<endl;

cout<<"Result from Efficient Iterative Method: "<<gcdIterativeEfficient(num1, num2)<<endl;

cout<<"Result from Recursive Method: "<<gcdRecursive(num1, num2)<<endl;

cout<<"Result from Efficient Recursive Method: "<<gcdRecursiveEfficient(num1, num2)<<endl;

return 0;
}

/*
Let see all above method using example
p = 15, q = 25;

First function: gcdIterative

check p != q =>true enter in while loop
iteration 1
Here q>p then
q = q-p => 25-15 = 10

Now p = 15, q = 10
check p != q =>true enter in while loop
iteration 2
Here p>q then
p = p-q => 15-10 = 5

Now p = 5, q = 10
check p != q =>true enter in while loop
iteration 3
Here p<q then
q = q-p => 10-5 = 5

Now p = 5, q = 5
check p != q => false so get out from while loop

Now return p => 5


Second Function gcdIterativeEfficient

p = 15, q = 25;

check q >0 => true
Enter to while loop:
1st Iteration
rem = p%q; => 15%25 => 15
p=q => p =25
q=rem => q = 15

check q >0 => true
Enter to while loop:
2nd Iteration
rem = p%q; => 25%15 => 10
p=q => p =15
q=rem => q = 10

check q >0 => true
Enter to while loop:
3rd Iteration
rem = p%q; => 15%10 => 5
p=q => p = 10
q=rem => q = 5


check q >0 => true
Enter to while loop:
4th Iteration
rem = p%q; => 10%5 => 0
p=q => p = 5
q=rem => q = 0


check q >0 => false

then return p => 5;


Third function gcdRecursive

gcdRecursive(15,25)
||
gcdRecursive(15,10) = > gcdRecursive(p,q-p)

||
gcdRecursive(5,10) => gcdRecursive(p-q,q)
||
gcdRecursive(5,5) => gcdRecursive(p, q-p)

Now p == q =>true
return p => 5


Note: Name your variable as your pseudo code


*/

Add a comment
Know the answer?
Add Answer to:
can somebody help me with this exercise 5 Euclidean algorithm The largest common divisor (gcd) of...
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
  • IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd)...

    IN PYTHON Write a recursive function for Euclid's algorithm to find the greatest common divisor (gcd) of two positive integers. gcd is the largest integer that divides evenly into both of them. For example, the gcd(102, 68) = 34. You may recall learning about the greatest common divisor when you learned to reduce fractions. For example, we can simplify 68/102 to 2/3 by dividing both numerator and denominator by 34, their gcd. Finding the gcd of huge numbers is an...

  • 1) Use the Euclidean algorithm (write in pseudocode) to find the greatest common divisor of 8...

    1) Use the Euclidean algorithm (write in pseudocode) to find the greatest common divisor of 8 898 and 14 321. 2) Program the Euclidean algorithm in 1) by using C++ programming language. 3) What is the greatest common divisor of 8 898 and 14 321? 4) Next, extend the Euclidean algorithm (write in pseudocode) to express gcd(8 898; 14 321) as a linear combination of 8 898 and 14 321. 5) Continue the programming in 2) to program the Extended...

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

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

  • C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers...

    C++ Problem 1 Write a function to calculate the greatest common divisor (GCD) of two integers using Euclid’s algorithm (also known as the Euclidean algorithm). Write a main () function that requests two integers from the user, calls your function to compute the GCD, and outputs the return value of the function (all user input and output should be done in main ()). In particular, you will find this pseudocode for calculating the GCD, which should be useful to you:...

  • Can someone please help me with these questions? There is one example given. The solution should...

    Can someone please help me with these questions? There is one example given. The solution should be like that. Necessary lines of codes which can run in dr.Racket. thank you!! Here is an example of using the llisting package to display code. Check the preamble to see where this is imported and set up. After that you will see a sample interaction from the DrRacket console to show how this function could be tested. You should provide similar listings and...

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
ADVERTISEMENT