Saturday, January 31, 2015

Beckett.java in pgm



Beckett.java

public class Beckett
 {
 public static void moves(int n, boolean forward) 
 {
        if (n == 0) return;
        moves(n-1, true);
        if (forward) System.out.println("enter " + n);
        else         System.out.println("exit  " + n);
        moves(n-1, false);
    }
                public static void main(String[] args) 
                {
        int N = Integer.parseInt(args[0]);
        moves(N, true);
    }
}

TowersOfHanoi.java in a pgm



TowersOfHanoi.java

public class TowersOfHanoi 
{
              public static void moves(int n, boolean left) 
             {
        if (n == 0) return;
        moves(n-1, !left);
        if (left) System.out.println(n + " left");
        else      System.out.println(n + " right");
        moves(n-1, !left);
    }
                public static void main(String[] args) 
               {
        int N = Integer.parseInt(args[0]);
        moves(N, true);
    }
}

Euclid.java in program



Euclid.java

public class Euclid 
{
 public static int gcd(int p, int q) 
{
        if (q == 0) return p;
        else return gcd(q, p % q);
    }
                public static int gcd2(int p, int q) 
              {
        while (q != 0) 
         {
            int temp = q;
            q = p % q;
            p = temp;
        }
        return p;
    }
 public static void main(String[] args)
 {
        int p = Integer.parseInt(args[0]);
        int q = Integer.parseInt(args[1]);
        int d  = gcd(p, q);
        int d2 = gcd2(p, q);
        System.out.println("gcd(" + p + ", " + q + ") = " + d);
        System.out.println("gcd(" + p + ", " + q + ") = " + d2);
    }
}