Need help with emirp problem.
I have two problems. First, the output has single digit numbers in it which I believe makes them not emirps (because they are the same backwards and forwards). The second problem I'm having is in formatting. I tried several variations of the printf(%3d, n) variety and could not get them to format properly. It has to be the 1st 100 emirps, 10 per line, each row right justified. The example output is:
13 17 31 37 71 73 79 97 107 113
149 157 167 179 199 311 337 347 359 389
Any help would be greatly appreciated.
Re: Need help with emirp problem.
Forgot to put my code in there:
public class Exercise05_27 {
public static void main (String [] args) {
int count=0;
int n=2;
while (count<100){
if (isEmirp(n)){
System.out.print(n+" ");
count++;
if (count%10==0)System.out.println();
}
n++;
}
}
public static boolean isPrime(int n){
for(int i=2; i<=n/2; i++){
if(n%i==0)return false;
}
return true;
}
public static boolean isEmirp(int n){
return isPrime(n) && isPrime(reverseDigits(n));
}
public static int reverseDigits(int n){
if (n<10) return n;
return recursiveHelper(n%10,n/10);
}
public static int recursiveHelper(int p, int q){
if(q<1) return p;
return recursiveHelper(p*10+q%10,q/10);
}
}
Re: Need help with emirp problem.
Okay, I just looked at the output I typed in and it isn't formatted the way I typed it. It should be all lined up, 10 per line, each column right justified.