-
-
Save bytecodeman/3413b5197a7663b0eee742aa144cdda2 to your computer and use it in GitHub Desktop.
6.26 Palindromic Prime Solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// My Solution to 6.26 (Student Problem) | |
package chapter6; | |
public class PalindromicPrime { | |
private static boolean isPrime(int num) { | |
for (int i = 2; i <= (int)(Math.sqrt(num)); i++) | |
if (num % i == 0) | |
return false; | |
return true; | |
} | |
private static int reverseInt(int num) { | |
int revInt = 0; | |
int sign = 1; | |
if (num < 0) { | |
sign = -1; | |
num = -num; | |
} | |
while (num > 0) { | |
int lastDigit = num % 10; | |
revInt = 10 * revInt + lastDigit; | |
num /= 10; | |
} | |
return sign * revInt; | |
} | |
private static boolean isPalindromic(int num) { | |
return num == reverseInt(num); | |
} | |
public static void main(String[] args) { | |
int count = 0; | |
int num = 2; | |
while (count < 100) { | |
if (isPrime(num) && isPalindromic(num)) { | |
System.out.printf("%7d ", num); | |
count++; | |
if (count % 10 == 0) | |
System.out.println(); | |
} | |
num++; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment