Question

In: Computer Science

java euclidean algorithm (1) Let a = 35, and b = -49. Please compute GCD(a, b)....

java

euclidean algorithm

(1) Let a = 35, and b = -49. Please compute GCD(a, b).

(2) Let a = 52, and b = 3. Please compute the quotient and remainder of a/b.

(3) Let a = -94, and b = 7. Please compute the quotient and remainder of a/b.

(4) Let a = 123, and b = 22. Please compute a mod b.

(5) Let a = -204, and b = 17. Please compute a mod b.

Solutions

Expert Solution

public class GCDEuclidean {

    public static int gcd(int n1, int n2) {
        n1 = (n1 > 0) ? n1 : -n1;
        n2 = (n2 > 0) ? n2 : -n2;
        while (n1 != n2) {
            if (n1 > n2)
                n1 -= n2;
            else
                n2 -= n1;
        }
        return n1;
    }

    public static void main(String[] args) {
        System.out.println("gcd(35, -49) is " + gcd(35, -49));
        System.out.println("a = 52, b = 3, quotient = " + (52/3) + ", remainder = " + (52%3));
        System.out.println("a = -94, b = 7, quotient = " + (-94/7) + ", remainder = " + (-94%7));
        System.out.println("a = 123, b = 22, a mod b = " + (123 % 22));
        System.out.println("a = -204, b = 17, a mod b = " + (-204 % 17));
    }
}


Related Solutions

a. Using the Euclidean Algorithm and Extended Euclidean Algorithm, show that gcd(99; 5) = 1 and...
a. Using the Euclidean Algorithm and Extended Euclidean Algorithm, show that gcd(99; 5) = 1 and find integers s1 and t1 such that 5s1 + 99t1 = 1. [Hint: You should find that 5(20) + 99(?1) = 1] b. Solve the congruence 5x 17 (mod 99) c. Using the Chinese Remainder Theorem, solve the congruence x 3 (mod 5) x 42 (mod 99) d. Using the Chinese Remainder Theorem, solve the congruence x 3 (mod 5) x 6 (mod 9)...
Use the Euclidean algorithm to find the GCD of 3 + 9i and 7-i
Use the Euclidean algorithm to find the GCD of 3 + 9i and 7-i
All necessary steps much show for these problems, please. Use the Euclidean algorithm to find gcd(12345,...
All necessary steps much show for these problems, please. Use the Euclidean algorithm to find gcd(12345, 54321). Write gcd(2420, 70) as a linear combination of 2420 and 70. The work to obtain the gcd is provided. 2420 = 34(70) + 40 70 = 1(40) + 30 40 = 1(30) + 10 30 = 3(10) + 0 Determine if 1177 is prime or not. If it is not, then write 1177 as a product of primes Find gcd(8370, 465) by unique...
Sample Execution : ** Recursive Euclidean Algorithm to calculate GCD ** Type a positive integer for...
Sample Execution : ** Recursive Euclidean Algorithm to calculate GCD ** Type a positive integer for A: -2 Sorry, you must enter a positive integer (>0). Please try again. ** Recursive Euclidean Algorithm to calculate GCD ** Type a positive integer for A: 35 Type a positive integer for B: 63 The GCD is 7 Write a MIPS assembly program that prompts the user for 2 positive integers (>0). Then it uses the Recursive Euclidean Algorithm to calculate GCD (the...
find a linear combination for gcd(259,313). use extended euclidean algorithm. what is inverse of 259 in...
find a linear combination for gcd(259,313). use extended euclidean algorithm. what is inverse of 259 in z subscript 313? what is inverse of 313 in z subscript 259?
Suppose a > b are natural numbers such that gcd(a, b) = 1. Compute each quantity...
Suppose a > b are natural numbers such that gcd(a, b) = 1. Compute each quantity below, or explain why it cannot be determined (i.e. more than one value is possible). (a) gcd(a3, b2) (b) gcd(a + b, 2a + 3b) (c) gcd(2a,4b)
(A) Let a,b,c∈Z. Prove that if gcd(a,b)=1 and a∣bc, then a∣c. (B) Let p ≥ 2....
(A) Let a,b,c∈Z. Prove that if gcd(a,b)=1 and a∣bc, then a∣c. (B) Let p ≥ 2. Prove that if 2p−1 is prime, then p must also be prime. (Abstract Algebra)
Let G be a group of order mn where gcd(m,n)=1 Let a and b be elements...
Let G be a group of order mn where gcd(m,n)=1 Let a and b be elements in G such that o(a)=m and 0(b)=n Prove that G is cyclic if and only if ab=ba
3. (4 marks) Let a and b be positive integers. Is gcd(5a + b, 11a +...
3. Let a and b be positive integers. Is gcd(5a + b, 11a + 2b) = gcd(2a + b, 3a + 2b)? If yes provide a proof. If not, provide a counterexample.
1. . Let A = {1,2,3,4,5}, B = {1,3,5,7,9}, and C = {2,6,10,14}. a. Compute the...
1. . Let A = {1,2,3,4,5}, B = {1,3,5,7,9}, and C = {2,6,10,14}. a. Compute the following sets: A∪B, A∩B, B∪C, B∩C, A\B, B\A. b. Compute the following sets: A∩(B∪C), (A∩B)∪(A∩C), A∪(B∩C), (A∪B)∩(A∪C). c. Prove that A∪B = (A\B)∪(A∩B)∪(B\A). 2. Let C0 = {3n : n ∈ Z} = {...,−9,−6,−3,0,3,6,9,...} C1 = {3n+1 : n ∈ Z} = {...,−8,−5,−2,1,4,7,10,...} C2 = {3n+2 : n ∈ Z} = {...,−7,−4,−1,2,5,8,11,...}. a. Prove that the sets C0, C1, and C2 are pairwise disjoint....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT