Question

(a) Show that the following algorithm computes the greatest common divisor g of the positive integers...

(a) Show that the following algorithm computes the greatest common divisor g of the positive integers a and b, together with a solution (u, v) in integers to the equation au + bv = gcd(a, b). 1. Set u = 1, g = a, x = 0, and y = b 2. If y = 0, set v = (g − au)/b and return the values (g, u, v) 3. Divide g by y with remainder, g = qy + t, with 0 ≤ t < y 4. Set s = u − qx 5. Set u = x and g = y 6. Set x = s and y = t 7. Go To Step (2) (b) Implement the above algorithm on a computer using the computer language of your choice. (c) Use your program to compute g = gcd(a, b) and integer solutions to the equation au + bv = g for the following pairs (a, b). (i) (527, 1258) (ii) (228, 1056) (iii) (163961, 167181) (iv) (3892394, 239847) (d) What happens to your program if b = 0? Fix the program so that it deals with this case correctly. (e) It is often useful to have a solution with u > 0. Modify your program so that it returns a solution with u > 0 and u as small as possible. [Hint. If (u, v) is a solution, then so is (u + b/g, v − a/g).] Redo (c) using your modified program.

Homework Answers

Answer #1
public class GCD {
   
    public static void main(String args[]){
             //Enter two number whose GCD needs to be calculated.      
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter first number to find GCD");
        int number1 = scanner.nextInt();
        System.out.println("Please enter second number to find GCD");
        int number2 = scanner.nextInt();
      
        System.out.println("GCD of two numbers " + number1 +" and " 
                           + number2 +" is :" + findGCD(number1,number2));
      
      
    }

    private static int findGCD(int number1, int number2) {
        //base case
        if(number2 == 0){
            return number1;
        }
        return findGCD(number2, number1%number2);
    }
  
}
Know the answer?
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
Similar Questions
Given that A to Z are mapped to integers 0-25 as follows. A:0, B:1, C:2, D:3,...
Given that A to Z are mapped to integers 0-25 as follows. A:0, B:1, C:2, D:3, E:4, F:5, G:6, H:7, I: 8, J: 9, K:10, L:11, M:12, N:13, O:14, P:15, Q:16, R:17, S:18, T:19, U:20, V:21, W:22, X:23, Y:24, Z:25. Encrypt the following message using Vigenere Cipher with key: CIPHER THISQUIZISEASY What is the ciphertext? Show your work. PLEASE HELP
Use Gaussian elimination to find the complete solution to the following system of​ equations, or show...
Use Gaussian elimination to find the complete solution to the following system of​ equations, or show that none exists. {-x + y + z = -1 {-x + 5y -15z = -29 { 7x - 6y - 11z = 0 Find the​ row-echelon form of the matrix for the given system of equations. ​(Do not include the vertical bar in the augmented​ matrix.) Select the correct choice below​ and, if​ necessary, fill in the answer boxes to complete your choice....
Use Gaussian elimination to find the complete solution to the following system of​ equations, or show...
Use Gaussian elimination to find the complete solution to the following system of​ equations, or show that none exists. {-x + y + z = -2 {-x + 5y -19z = -30 { 7x - 5y - 17z = 0 Find the​ row-echelon form of the matrix for the given system of equations. ​(Do not include the vertical bar in the augmented​ matrix.) Select the correct choice below​ and, if​ necessary, fill in the answer boxes to complete your choice....
The following algorithm finds the initial substring of y that can be reversed and found in...
The following algorithm finds the initial substring of y that can be reversed and found in y. For example, longestInitialReverseSubstringLength(“aabaa”) = 5, because “aabaa” is the same string forwards and backwards, so the longest initial substring that can be reversed and found in the string is “aabaa”. Also, longestInitialReverseSubstringLength(“bbbbababbabbbbb”) is 6, because “babbbb” can be found in the string (see color-highlighted portions of the string), but no longer initial string exists reversed in any part of the string. longestInitialReverseSubstringLength(String y)...
Important Instructions: (1) λ is typed as lambda. (2) Use hyperbolic trig functions cosh(x) and sinh(x)...
Important Instructions: (1) λ is typed as lambda. (2) Use hyperbolic trig functions cosh(x) and sinh(x) instead of ex and e−x. (3) Write the functions alphabetically, so that if the solutions involve cos and sin, your answer would be Acos(x)+Bsin(x). (4) For polynomials use arbitrary constants in alphabetical order starting with highest power of x, for example, Ax2+Bx. (5) Write differential equations with leading term positive, so X′′−2X=0 rather than −X′′+2X=0. (6) Finally you need to simplify arbitrary constants. For...
3. Consider the region R in the first quadrant enclosed by y = x, y =...
3. Consider the region R in the first quadrant enclosed by y = x, y = x/2, and y = 5. (a) Sketch this region, making sure to identify and label all points of intersection. (b) Find the area of R, using the method of your choice. (c) Using the method of your choice, set up an integral for the volume of the solid resulting from rotating R around the y-axis. Do NOT evaluate the integral. (d) Using the method...
Write a program of wordSearch puzzle that use the following text file as an input. The...
Write a program of wordSearch puzzle that use the following text file as an input. The output should be like this: PIXEL found (left) at (0,9). ( Use JAVA Array ) .Please do not use arrylist and the likes! Hints • The puzzle can be represented as a right-sized two-dimensional array of characters (char). • A String can be converted into a right-sized array of characters via the String method toCharArray. . A word can occur in any of 8...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
pseudocode please!! Assignment6C: P0\/\/|\|3D. In the early 80s, hackers used to write in an obfuscated, but...
pseudocode please!! Assignment6C: P0\/\/|\|3D. In the early 80s, hackers used to write in an obfuscated, but mostly readable way called “leet” – short for “elite”. In essence, it was a simple character replacement algorithm, where a single “regular” character was replaced by one or more “leet” characters; numbers remained the same. Here’s one of the most readable versions: a 4 g 9 m /\\/\\ s $ y ‘/ b B h |-| n |\\| t 7 z Z c (...
1. Vim commands: a. How do you auto indent your program? b. Explain what the following...
1. Vim commands: a. How do you auto indent your program? b. Explain what the following commands do: dd, y3, p, :set cindent (1 pt) VIM exercises These exercises on the computer need to be repeated by each student in the pair. This is to ensure that both students understand how to get around in Linux!!! For this part of the lab, you will create a .vimrc file that will help you develop your C++ programs using VIM. First, we...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT