Assignment One

Loop is an important construct in building programs. Construct correct "while" loops to solve the following problems. Write all the programs in Java. Please write the loop invariant as comments in your code. Justify your code using the loop invariant and the loop condition. Test your code and verify it works before submitting your code to Canvas.

Note that your Java file name needs to be the same as your public class name in the file. Otherwise, Java reports error.

  1. Euclid algorithm for Greatest Common Divisor (GCD). For two positive integers, if a > b, we have GCD(a, b) = GCD(a-b, b). Note that GCD(a,a) = a. Construct a loop invariant from this fact. Write a loop to compute GCD based on the above observations. Write a Java function to implement your idea.
  2. Write a Java function to reverse an non-negative integer For example, for the input 123919, the output should be 919321 and for the input 12340, the output should be 4321. We assume the input number never start from a 0 if the number itself is not 0. The input itself can be 0. Use while loop instead of recursion to solve the problem.
  3. Write a faster method to compute square root. In our class, we have written a loop to compute the square root of integer N>=0 so that x^2 <= N && (x+1)^2 > N. Use the loop invariant {I: x is in range [a, b]} and the inverse loop condition {!B : a == b} to derive a faster solution. Try to half the size of the range during each loop. Hint: the initial range of [a, b] can be set to [0, N]. In the loop, let c = floor((a+b)/2), choose an interval [a, c-1], [c,c] or [c+1, b] so that x is guaranteed to be there.

Solution

  1. 
    public class Gcd {
    
         public static int gcd(int a, int b)
         {
            if (a == 0) return b;
            if (b == 0) return a;
    
            // {I: gcd(a, b) == gcd(a0, b0), where a0 and b0 
            //     are inital values of a and b }
            while (a != b)
            {
               if (a > b)
                  a = a - b;
               else
                  b = b - a;
            }
    
            return a;
         }
    
         public static void main(String[] args)
         {
             System.out.println(gcd(35, 21));
             System.out.println(gcd(16, 40));
         }
    }
    
  2. 
    public class Reverse {
    
         public static int reverse(int x)
         {
            int r = 0;
            // {I: x is the value of the part that has not been reversed
            //     r is the reversed value of processed part }
            while (x != 0)
            {
               int lowest_digit = x % 10;
               x = x / 10;
               r = r * 10 + lowest_digit;
            }
            return r;
         }
    
         public static void main(String[] args)
         {
             System.out.println(reverse(123456));
             System.out.println(reverse(12340));
             System.out.println(reverse(12034));
         }
    }
       
  3. 
    public class Sqrt {
    
         public static int sqrt(int x)
         {
            int a = 0;
            int b = x;
            // {I: sqrt(x) is always in [a, b] }
            while (a != b)
            {
               int c = (a+b)/2;
               if (c*c <= x && (c+1)*(c+1) > x)
                  return c;
    
               if (c*c > x)
                  b = c - 1;
               else
                  a = c + 1;
            }
    
            return a;
         }
    
         public static void main(String[] args)
         {
             System.out.println(sqrt(101));
             System.out.println(sqrt(64));
             System.out.println(sqrt(0));
         }
    }