Showing posts with label Logic. Show all posts
Showing posts with label Logic. Show all posts

Thursday, June 21, 2012

Magic Square



public class Magic {
      private int size = 3;

      public static void main(String[] args) {
            Magic magic = new Magic(1);
            magic.magic(1);
      }

      public Magic (int size) {
            setSize(2 * size + 1);
      }
     
      public void magic(int num) {
            int limit = getLimit();
            int current = getSize() / 2;

            int[] a = new int[limit];
            a[current] = num;
            int next = 0;

            for (int i = num + 1; i < num + limit; i++) {
                  next = next(current);
                  if (a[next] == 0) {
                        a[next] = i;
                  } else {
                        next = down(current);
                        a[next] = i;
                  }
                  current = next;
            }
            display(a);
      }
     
      public void display(int[] a){
            for(int i=0; i
                  if(i%getSize()==0){
                        System.out.println();
                  }
                  System.out.printf("%5d", a[i]);
            }
                 
      }

      public int next(int i) {
            return right(up(i));
      }

      public int up(int i) {
            i = i - getSize();
            if (i < 0) {
                  i += getLimit();
            }
            return i;
      }

      public int down(int i) {
            i = i + getSize();
            if (i > getLimit()) {
                  i -= getLimit();
            }
            return i;
      }

      public int right(int i) {
            i = i + 1;
            if (i % getSize() == 0) {
                  i = i - getSize();
            }
            return i;
      }

      public int left(int i) {
            if (i % getSize() == 0) {
                  i = i + getSize() - 1;
            } else {
                  i = i - 1;
            }
            return i;
      }

      public int getSize() {
            return size;
      }

      public void setSize(int size) {
            this.size = size;
      }

      public int getLimit() {
            return getSize() * getSize();
      }
}

Wednesday, May 9, 2012

Checking Prime in java


      public class Prime {
            public boolean isPrime(int n) {
                  if (n < 2) {
                      return false;
                  }
                  if (n == 2) {
                      return true;
                  }
                  if (n % 2 == 0) {
                      return false;
                  }

                  int sqrt = (int) Math.sqrt(n);

                  for (int i = 3; i <= sqrt; i += 2) {
                        if (n % i == 0) {
                           return false;
                        }
                  }
                  return true;
            }
      }

Tuesday, May 8, 2012

String Pallendrom Checking in Java


public class Pallendrom {

      public boolean isPallendrom(String str) {
            char[] chars = str.toCharArray();
            int lchar = chars.length;
            for(int i=0; i<lchar/2; i++){
                  if(chars[i]!=chars[lchar-1-i]){
                        return false;
                  }
            }
            return true;
      }

      public static void main(String[] args) {
            String a = "12321";
            Pallendrom p = new Pallendrom();
            if(p.isPallendrom(a)){
                  System.out.println(a + " is Pallendrom.");
            }else{
                  System.out.println(a + " is not Pallendrom.");
            }
      }
}


Number Pallendrom checking in java


public class Pallendrom {

      public boolean isPallendrom(int num) {
            int tmp = num;
            int rem = 0;
            int sum = 0;

            while (tmp > 0) {
                  rem = tmp % 10;
                  sum = sum * 10 + rem;
                  tmp = tmp / 10;
            }
            return (sum == num);
      }

      public static void main(String[] args) {
            int a = 12321;
            Pallendrom p = new Pallendrom();
            if(p.isPallendrom(a)){
                  System.out.println(a + " is Pallendrom.");
            }else{
                  System.out.println(a + " is not Pallendrom.");
            }
      }
}

Finding Square root without using sqrt function in java


public class Maths {

      public double sqrt(double number) {
            if (number < 0) {
                  return Double.NaN;
            }
            if (number == 0) {
                  return number;
            }
            double epsilon = 1e-15;
            double root = 0;
            double sqrt = number;
            while (Math.abs(sqrt - (root = (number / sqrt))) > epsilon * sqrt) {
                  sqrt = (root + sqrt) / 2.0;
            }
            return sqrt;
      }

      public static void main(String[] args) {
            double num = 2;
            Maths m = new Maths();
            double sqrt = m.sqrt(num);
            System.out.println("SQRT is " + sqrt);
      }
}

LCM implementation in Java


public class Maths {
      public int lcm(int num1, int num2) {
            if (num1 == 0 || num2 == 0) {
                  return 0;
            }
            if (num1 < num2) {
                  num1 = num1 ^ num2;
                  num2 = num1 ^ num2;
                  num1 = num1 ^ num2;
            }
            int num = num1 % num2;
            if (num == 0) {
                  return num1;
            }

            for (int i = 2; i <= num; i++) {
                  if ((num1 * i) % num2 == 0) {
                        return i * num1;
                  }
            }
            return num1 * num2;
      }

      public static void main(String[] args) {
            int a = 24;
            int b = 16;

            Maths m = new Maths();
            int l = m.lcm(a, b);
            System.out.println("LCM is " + l);
      }
}

GCD implementation in Java


public class Maths {

      public int gcd(int p, int q) {
            if (q == 0){
                  return p;
            }
            return gcd(q, p % q);
      }

      public static void main(String[] args) {
            int a = 36;
            int b = 48;
           
            Maths m = new Maths();
            int g = m.gcd(a,b);
            System.out.println("GCD is " + g);
      }
}

Factorial in Java


public class Factorial {

      public static void main(String[] args) {
            Factorial f = new Factorial();
            int num = 6;
            int fact = f.factorial(6);
            System.out.println(num + " factorial is " + fact);
      }

      public int factorial(int n) {
            if (n == 1)
                  return 1;
            return n * factorial(n - 1);
      }
}