Results 1 to 5 of 5
  1. #1
    javanewbie123 is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default 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:
    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;
    	}
    }
    how would I go about combining them? I tried changing up the main but still doesn't work.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Palindromic Prime - Combining isPrime and isPalindrome

    Could you call one method and if it returns true, then call the other?

  3. #3
    javanewbie123 is offline Member
    Join Date
    Mar 2012
    Posts
    2
    Rep Power
    0

    Default Re: Palindromic Prime - Combining isPrime and isPalindrome

    not sure how to do that. could you demonstrate?

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Palindromic Prime - Combining isPrime and isPalindrome

    Method to check if a number is a palindrome
    Java 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
    Java 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
    Java Code:
    public List<Integer> getANumberOfPrimes(int number) {
        ...
    }
    Last edited by ozzyman; 03-14-2012 at 01:52 AM. Reason: correction of my excessive misspelling of palindrome

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default 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

Similar Threads

  1. Palindromic Prime?
    By soccergirl67 in forum New To Java
    Replies: 5
    Last Post: 12-01-2011, 10:48 AM
  2. Combining shapes?
    By snj00u in forum Java 2D
    Replies: 2
    Last Post: 06-14-2011, 10:09 PM
  3. How to print palindromic numbers on the console!
    By AlfieSDK in forum New To Java
    Replies: 6
    Last Post: 05-21-2011, 03:24 AM
  4. Combining these 2 projects
    By fresh83 in forum New To Java
    Replies: 14
    Last Post: 12-28-2009, 09:52 AM
  5. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 02:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •