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
  • 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⁡(u1,u2,….un). One way to do it is to assume all numbers are non-negative, so if only one of if uj≠0 it is the gcd. Otherwise replace uk by uk mod uj  for all k≠j where uj is the minimum of the non-zero elements (u’s). The algorithm can be made significantly faster if one...

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

  • a Find the greatest common divisor (gcd) of 322 and 196 by using the Euclidean Algorithm....

    a Find the greatest common divisor (gcd) of 322 and 196 by using the Euclidean Algorithm. gcd- By working back in the Euclidean Algorithm, express the gcd in the form 322m196n where m and n are integers b) c) Decide which of the following equations have integer solutions. (i) 322z +196y 42 ii) 322z +196y-57

  • Write down the Euclidean algorithm then use the algorithm to find the greatest common divisor of...

    Write down the Euclidean algorithm then use the algorithm to find the greatest common divisor of the following pairs of numbers. 315, 825 2091, 4807

  • 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: Divide n1 by n2 and let r be the remainder. 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.) Set n1 to the value of n2, set n2...

  • 20 points Problem 4: Extended Euclidean Algorithm Using Extended Euclidean Algorithm compute the greatest common divisor...

    20 points Problem 4: Extended Euclidean Algorithm Using Extended Euclidean Algorithm compute the greatest common divisor and Bézout's coefficients for the pairs of integer numbers a and b below. Express the greatest common divisor as a linear combination with integer coefficients) of a and b. (Do not use factorizations or inspection. Please demonstrate all steps of the Extended Euclidean Algo- rithm.) (a) a 270 and b = 219 (b) a 869 and b 605 (c) a 4930 and b-1292 (d)...

  • Question 1. (a) Find the greatest common divisor of 10098 and 3597 using the Euclidean Algorithm....

    Question 1. (a) Find the greatest common divisor of 10098 and 3597 using the Euclidean Algorithm. (b) Find integers a and a2 with 1009801 +3597a2 = gcd(10098,3597). (c) Are there integers bı and b2 with 10098b1 + 3597b2 = 71? Justify your answer. (d) Are there integers ci and c2 with 10098c1 + 3597c2 = 99? Justify your answer. Question 2. Consider the following congruence. C: 21.- 34 = 15 (mod 521) (a) Find all solutions x € Z to...

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

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

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

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