VMWare Inc Interview Question
SDETsCountry: India
Interview Type: Phone Interview
public static void checkPrime(String str){
// convert to decimal
int x=Integer.parseInt(str,2);
System.out.print(" Input number:" + x );
// check if mod is
if (x ==2 ) { System.out.println(" Prime number"); return ;}
for(int i =2; i <= Math.sqrt(x); i++ )
if (x %i == 0) {
System.out.println(" No Prime number:" + i );
return ;
}
System.out.println(" Prime number");
}
public static void checkPrime(String str){
// convert to decimal
int x=Integer.parseInt(str,2);
System.out.print(" Input number:" + x );
// check if mod is
if (x ==2 ) { System.out.println(" Prime number"); return ;}
for(int i =2; i <= Math.sqrt(x); i++ )
if (x %i == 0) {
System.out.println(" No Prime number:" + i );
return ;
}
System.out.println(" Prime number");
}
we can convert into it's decimal representation and check in O(sqrt(N)) if it's prime or not. Just to speed up things a little, we can get rid of even numbers just by checking the rightmost bit.
- Darkhan.Imangaliev May 26, 2015