Palindromic Prime - Combining isPrime and isPalindrome
Hi guys I am somewhat new to Java programming and am looking for some help. I have 2 programs, isPrime and isPalindrome, and I would like to combine them so that the new program displays the first 100 prime numbers that are palindromes. I am having a hard time doing this and was wondering if anyone could help me. Thanks
isPrime Code:
Code:
public class ShowPrimes
{
static final int TOTAL_PRIMES_TO_FIND = 100; // number of primes to be found in this application
static final int PRIMES_SHOWN_PER_LINE = 10; // number of primes shown per display line
public static void main(String[] args)
{
int lastPrimeFound; // last prime number found via test
int count; // count of number of primes found
System.out.println("Here are the first 100 prime numbers: ");
System.out.printf("%5d%5d", 2,3); // clearly 2 and 3 are prime numbers!
lastPrimeFound = 3; // our first number to test
for (count = 2; count < TOTAL_PRIMES_TO_FIND; )
{
++count; // count of the prime we are presently finding
lastPrimeFound = getNextPrime(lastPrimeFound); // candidate for next prime from previous prime
System.out.printf("%5d", lastPrimeFound); // prime found, so display it
if (count % PRIMES_SHOWN_PER_LINE == 0)
System.out.printf("\n");
}
}
// returns the next prime number using value of primeJustFound to generate a next candidate
private static int getNextPrime(int primeJustFound)
{
int candidate; // candidate value tested for a prime
candidate = primeJustFound + 2;
while (!isPrime(candidate))
{
candidate += 2;
}
return candidate;
}
// returns true if testValue is prime and false if testValue is divisible by some number other than 1 or itself
private static boolean isPrime(int possiblePrime)
{
boolean factorFound = false; // set to true if loop finds a factor
double limit = Math.sqrt(possiblePrime); // upper limit on integers to test
int possibleFactor; // int value that could be a factor of possiblePrime
possibleFactor = 3; // even numbers are not part of the logic
while (possibleFactor <= limit && !factorFound) // exits with factor found or no more factors to test
{
if (possiblePrime % possibleFactor == 0) // factor found; no longer prime
factorFound = true;
else
possibleFactor+= 2;
}
return !factorFound;
}
}
isPalindrome Code:
Code:
import java.util.*;
// Main is a driver for the method isPalindrome
public class TestIsPalindrome
{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
int testValue;
System.out.print("Enter a test value: ");
testValue = keyboard.nextInt( );
keyboard.nextLine( );
if (isPalindrome(testValue))
System.out.println("The number " + testValue + " is a palindrome.");
else
System.out.println("The number " + testValue + " is not a palindrome.");
}
// returns true if testValue is a palindrome; otherwise returns false
private static boolean isPalindrome(int testValue)
{
int reversedNumber; // least significant digit of testValue is most significant and vice-versa
int generatorForReversedNumber; // gotten by "extracting" and weighting each digit from testValue
int nextDigit; // to be extracted from testValue
reversedNumber = 0; // sum accumulator.. initially no digits accumulated
generatorForReversedNumber = testValue;
while (generatorForReversedNumber > 0)
{
nextDigit = generatorForReversedNumber % 10;
reversedNumber = 10 * reversedNumber + nextDigit;
generatorForReversedNumber /= 10;
}
return testValue == reversedNumber;
}
}
how would I go about combining them? I tried changing up the main but still doesn't work.
Re: Palindromic Prime - Combining isPrime and isPalindrome
Could you call one method and if it returns true, then call the other?
Re: Palindromic Prime - Combining isPrime and isPalindrome
not sure how to do that. could you demonstrate?
Re: Palindromic Prime - Combining isPrime and isPalindrome
Method to check if a number is a palindrome
Code:
public boolean isPalindrome(int number) {
//code to find out if it is a palindrome
... //if it is a palindrome, return true;
//otherwise:
return false;
}
The part of your code where you are checking for both palindrome and prime
Code:
List<Integer> listOfPrimes = getANumberOfPrimes(100);
for (int prime:listOfPrimes) {
if (isPalindrome(prime)) {
//it is both a palindrome and a prime
...
}
}
Method to get a number of primes
Code:
public List<Integer> getANumberOfPrimes(int number) {
...
}
Re: Palindromic Prime - Combining isPrime and isPalindrome
while count < 100
get next prime
is it a palindrome?
save it and incr the count
end loop