Question

We discuss the Euclidean algorithm that finds the greatest common divisor of 2 numbers u and...

We discuss the Euclidean algorithm that finds the greatest common divisor of 2 numbers u and v. We want to extend and compute the gcd of n integers gcd⁡(u_1,u_2,….u_n). One way to do it is to assume all numbers are non-negative, so if only one of if u_j≠0 it is the gcd. Otherwise replace u_k by u_k mod u_j for all k≠j where u_j is the minimum of the non-zero elements (u’s). The algorithm can be made significantly faster if one consider the identity: gcd⁡(u_1,u_2….u_n )=gcd⁡(u_1,gcd⁡(u_2,u_3,..u_n )). Then we may calculate gcd⁡(u_1,u_2….u_n ) with following algorithm: Step 1. set d←u_n,j←n-1 Step 2. if d≠1 and j>0 set gcd⁡(u_j,d)and j=j-1 and repeat Step 2. Else d=gcd(u_1,u_2,…u_n). What is the sma`llest value of u_n such that the calculation of: gcd(u_1,u_2,...,u_n) by steps Step 1 and Step 2 (given above) requires N divisions, if Euclid’s algorithm is used throughout? Assume that N ≥ n.

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

The following java code can be used to find the gcd of array of elements:

public class GCD{
static int gcd(int x,int y) //GCD of two numbers
{
if (x==0)
return y;
return gcd(y%x,x);
}
static int findgcd(int arr[], int n)//Function to find gcd of array of numbers. Ex:gcd⁡(u_1,u_2,…,u_n).
{
int result = arr[0];
for (int i=1;i<n;i++){
result=gcd(arr[i],result);

if(result==1)
{
return 1;
}
}
return result;
}
public static void main(String args[])
{
int arr[] = {2,4,6,26,45}; //Input elemets
//you can write a loop to get the inputs as per user requirement
int n=arr.length;
System.out.println(findgcd(arr,n));//Array elements and array length are passed as arguments
}
}

Add a comment
Know the answer?
Add Answer to:
We discuss the Euclidean algorithm that finds the greatest common divisor of 2 numbers u and...
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
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
Active Questions
ADVERTISEMENT