Results 1 to 5 of 5
- 03-13-2012, 09:43 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
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:
Java 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:
how would I go about combining them? I tried changing up the main but still doesn't work.Java 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; } }
- 03-13-2012, 10:27 PM #2
Re: Palindromic Prime - Combining isPrime and isPalindrome
Could you call one method and if it returns true, then call the other?
- 03-14-2012, 12:33 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 2
- Rep Power
- 0
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
The part of your code where you are checking for both palindrome and primeJava 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; }
Method to get a number of primesJava Code:List<Integer> listOfPrimes = getANumberOfPrimes(100); for (int prime:listOfPrimes) { if (isPalindrome(prime)) { //it is both a palindrome and a prime ... } }
Java Code:public List<Integer> getANumberOfPrimes(int number) { ... }Last edited by ozzyman; 03-14-2012 at 12:52 AM. Reason: correction of my excessive misspelling of palindrome
- 03-14-2012, 01:19 AM #5
Similar Threads
-
Palindromic Prime?
By soccergirl67 in forum New To JavaReplies: 5Last Post: 12-01-2011, 09:48 AM -
Combining shapes?
By snj00u in forum Java 2DReplies: 2Last Post: 06-14-2011, 09:09 PM -
How to print palindromic numbers on the console!
By AlfieSDK in forum New To JavaReplies: 6Last Post: 05-21-2011, 02:24 AM -
Combining these 2 projects
By fresh83 in forum New To JavaReplies: 14Last Post: 12-28-2009, 08:52 AM -
Prime Number - System print all the prime numbers ...
By pinkdreammsss in forum New To JavaReplies: 20Last Post: 04-26-2009, 01:50 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks