Saturday, January 3, 2015

Switch Operators Some Interesting facts

Note: The below understanding is a collection from numerous articles which have been browsed thru the web to get the understanding.
References will be provided at the end.

SWITCH CASE:
Since JavaSE7 onwards this is one statement which works with the following data-types-
  1. byte
  2. short
  3. int
  4. char
  5. enum types
This also works with following wrapper classes-
  1. Byte
  2. Short
  3. Integer
  4. Character
Some good features:
  • default statement is optional.
  • default statement can be at the beginning and before case statements
  • break statements are useful and recommended. If these are not used then next statements can get executed.
  • presence of default statement helps to handle conditions at run-time, which otherwise can return unexpected results.
  • One can use loops, if-then-else statements within case/default.
  • Multiple case statements can be clubbed together and one result generated. 
  • In case this is enclosed in a for, do-while or while loop, then a break followed by loop name can break the loop;

Saturday, October 18, 2014

Largest palindrome product

Ques:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.
Ans:
 This can be achieved in 2 differnet ways..
Ist: Declare as wrapper class integer
Convert to String
Use tochar to seperate the digits
Read them backwards
convert back to Int and get answer

I have used basics of math to arrive at solution.
Extract the digits using maths fundamentals
store them in variables.
re-arrange them using Maths fundamentals.

AND YOU HAVE THE ANSWER.
 ==========================

Package palindrome;

public class Palindrome {

  
    public static void main(String[] args) {
          int mult = 906609;
        for (int i = 100; i < 1000; i++) {
            for (int j = 100; j < 1000; j++) {
                mult = i * j;
                      if (mult - placeholdervalue(mult) == 0) {
                    System.out.println("Original number=" + mult + " and its palindrome=" + placeholdervalue(mult));
                    System.out.println("i=" + i + "j=" + j);
                }
            }

                   }
    }

    public static int placeholdervalue(int num) {

        int var0 = 0, var, var1, var2, var3, var4, var5, var6;
        var = num / 10000;

        if (var >= 10) {
            var0 = num / 100000;
            var = (num - var0 * 100000) / 10000;
        }
        var1 = (num - var * 10000 - var0 * 100000) / 1000;
        var2 = (num - var * 10000 - var0 * 100000 - var1 * 1000) / 100;
        var3 = (num - var * 10000 - var0 * 100000 - var1 * 1000 - var2 * 100) / 10;
        var4 = (num - var * 10000 - var0 * 100000 - var1 * 1000 - var2 * 100 - var3 * 10);
     
              var5 = var4 * (var0 > 0 ? 100000 : 10000);
        var5 = var5 + var3 * (var0 > 0 ? 10000 : 1000);
        var5 = var5 + var2 * (var0 > 0 ? 1000 : 100);
        var5 = var5 + var1 * (var0 > 0 ? 100 : 10);
        var5 = var5 + var * (var0 > 0 ? 10 : 1);
        var5 = var5 + var0;
        return var5;
    }

Wednesday, October 15, 2014

Highly divisible triangular number

Ques:
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
 1: 1
 3: 1,3
 6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred divisors?


Ans:

package highlydivisible;

public class HighlyDivisible {
 

    public static void main(String[] args) {
          int nthtriplet = 9;
        int tripdes = 500;
        int cnt = 0;
        int gettriplet = 0;
        boolean val=false;
        int[] obtaindivisor=new int[nthtriplet-1];
        int matchpos=0;
   
        for (int i = 2; i < 200000; i++) {
           
            gettriplet = findtriplet(i);
            obtaindivisor=getdivisor(gettriplet, i);
            for(int j:obtaindivisor){
                if(j!=0){
                    cnt++;
                }
                if(cnt==tripdes){
                    val=true;
                    matchpos=i;
                    break;
                }
            }

            if(val){
                System.out.println("the position is "+matchpos+" & triplet is "+findtriplet(i));
                break;
            }
            cnt=0;
        }
    }

    public static int findtriplet(int num) {
        int sump = 0;
        for (int i = 1; i <= num; i++) {
            sump += i;
        }
        return sump;
    }

    public static int[] getdivisor(int sumoftriplets, int uptonum) {
        int[] getd = new int[uptonum+2];
        int j = 0;
        for (int i = 1; i <= sumoftriplets; i++) {
              if (sumoftriplets % i == 0) {
                getd[j] = i;
                j++;     
            }
        }
        return getd;
    }
}

Monday, October 13, 2014

Summation of Primes

Ques:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.

Ans:
public static void main(String[] args) {
        int count = 0;
        int pcount=1;
        int tarprnum=10001;
        int sump=2;
        for (int i = 3; i < 2000000; i++) {
            count=0;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                           count++;
                    break;
                }
            }
            if(count==0){
                pcount++;
                sump+=i;
                           }
                 }
        System.out.println("sum="+sump);
    }

}

Sunday, October 12, 2014

10001st prime

Ques:
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
 
 
Ans:
Prime number is number divided by 1 or itself. 
1st Prime=2 is fixed. 2nd onwards we calculate as below........
============================================================
package isprime;

public class IsPrime {

        public static void main(String[] args) {
        int count = 0;
        int pcount=1;//counts Prime
        int tarprnum=10001;//target expected to be met
        for (int i = 3; i < 1000000000; i++) {
            count=0;
            for (int j = 2; j < i; j++) {
                if (i % j == 0) {
                    count++;
                    break;
                }
            }
            if(count==0){
                pcount++;
                    }
            if(pcount==tarprnum){
          System.out.println("The target prime num="+i);break;}
        }
    }

}

Pythagorean triplet

Ques:
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

Ans:

package gradesforeexam;


public class GradesforeExam {

     public static void main(String[] args) {
        double a=0, b=0, c = 0;
        for (int i = 1; i < 1000; i++) {
            for (int j=i+1;j<1000;j++ ) {
                c = Math.pow(Math.pow(i, 2) + Math.pow(j, 2), 0.5);
               
                if ((c - (int) c == 0.0)&&(i+j+(int)c==1000)) {
                   System.out.println(a*b*c);
                   }
            }
        }
    }

}

Saturday, October 11, 2014

noTeenSum

Ques:
Given 3 int values, a b c, return their sum. However, if any of the values is a teen -- in the range 13..19 inclusive -- then that value counts as 0, except 15 and 16 do not count as a teens. Write a separate helper "public int fixTeen(int n) {"that takes in an int value and returns that value fixed for the teen rule. In this way, you avoid repeating the teen code 3 times (i.e. "decomposition"). Define the helper below and at the same indent level as the main noTeenSum()

Ans:public int noTeenSum(int a, int b, int c) {
//  public static int noTeenSum(int a, int b, int c){
      
       return fixteen(a)+fixteen(b)+fixteen(c);
   }
    public int fixteen(int age){
        int helper=0;
        if(age>=13&&age<=19){
            if(age==15||age==16){
                helper=age;
       //         continue a;
            }else{helper=0;}
        }else helper=age;
          return helper;
    }