Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Binary Palindromes?

807603Feb 18 2008 — edited Feb 19 2008
Ok. I am still working on my program for my computer class and I am near the end. I have gotten past the part where I have to find prime and perfect numbers, now I have to convert every number from Interger.MAX_VALUE to binary and see if it is a palindrome. I need help with converting the numbers to binary. I got the method from wikipedia, but am not getting the correct result. I think I can get the palidrome part down, if someone could just help with the binary, thanks. I didn't know if I should post my whole program here or not, but I did anyways. I am testing 1000 as the max value just for time sake. Any help would be greatly appreciated! THANKS!!! P.S. The binary conversion is the last method, so it'll be at the bottom.

---I am getting "101" as the output for 3 which is not true and I just cant seem to fix it. I used debugger to find this information - I have no true output for palindrome or binary quite yet.

{code
/* This program's goal is to take every possible 'int' number, and classify / print all that are in the 
 * following: prime numbers, perfect, and palindrome (in binary form). This program uses loops and methods 
 * to accomplish this */

public class Project4 
{ // starting class
	// main method 
	public static void main(String[] args)
	{ // starting main
		// declare variables
		int NUMBER_LIMIT = 1000;
		
		// invoke prime number method
		primeNumber(NUMBER_LIMIT);
		
		// invoke perfect number method
		perfectNumber(NUMBER_LIMIT);
		
		// invoke palindrome method
		palindrome(NUMBER_LIMIT);
	}
	
	// prime number method
	public static void primeNumber(int limit)
	{ //starting primeNumber
		//declare variables
		int count = 0, // establishes a counter for the while loop
			number = 2, // is the number that is tested
			divisor = 2; // the number that is divided into the 'number' to see if it is prime
	
		// greeting showing that the following numbers shown are prime
		System.out.println("These are the possible prime numbers from every possible positive integer:");
	
		// starts the while loop to find prime numbers
		while(count < limit)
		{
			boolean isPrime = true; // all numbers are prime until proven false
		
			/* for-loop to decide whether a number is prime or not; uses Math.sqrt to make the 
			 * process faster by square rooting the 'test-number' because only a small part
			 * of the number needs to be tested (the divisor) against the number.
			 */
			for(divisor = 2; divisor <= Math.sqrt(number); divisor++)
			{
				/* if this condition is true, it takes the program out of the loop and shows that the 
				 * 'test-number' is not prime.
				 */
				if(number % divisor ==0)
				{
					isPrime = false; // 'test-number' is not prime
					break;  // takes program out of loop
				}
			}
		
			// tests that if the number is prime, displays the number
			if(isPrime)
			{
				System.out.println(number); //displays the number 
				count++;	// adds 1 to count
			}
			number++; // adds 1 to the 'test-number'
		}
	}// ends primeNumber
	
	// perfect number method
	public static void perfectNumber(int limit)
	{// starts perfectNumber
		
		/* because all numbers up to 10^50 are all even according to Dr.Math,
		* I think it is fair to say that all perfect numbers with Integer.MAX_VALUE are going to be even. 
		* So this do-while loop finds all the even numbers and while doing so
		* compares their divisors to zero. If all the divisors add up to the given number,
		* then the number is perfect.
		*/
		
		// declare variables
		int number = 1,
			add,
			divisor;
		
		// greeting saying that the following numbers are perfect
		System.out.println("The following numbers are perfect: ");
		
		// start do-while loop
		do
		{
			add = 0;
			// for-loop to compare the number with the divisor
			for(divisor = 1; divisor <= (number/2); divisor++)
			{ 
				if(number % divisor == 0)
				{
					add += divisor; // adds the divisors to 'add' to compare later with number to see if perfect
				}
			}
			// if statement comparing add to number to see if it is perfect 
			if(add == number)
			{
				System.out.println(number);
			}
			
			number++; // adds 1 to number
			
		} while(number < limit); // ending condition statement for do-while loop
	}// ends perfectNumber
	
	// palindrome method
	public static void palindrome(int limit)
	{// starts palindrome
		//declare variables
		int	i = 0;
		
		binaryConversion(limit);
		
		while(i < limit)
		{
			
		}
	}// ends palindrome
	
	// convert number to binary method
	public static String binaryConversion(int limit)
	{// starts binary
		// declare variables
		String number = "";
		int temp1 = 0;
		
		for(int count = 1; count <= limit; count++)
		{
			number = "";
			temp1 = 0;
			
			for(int temp = 1; temp < count; temp ++)
			{
				if(count != 1)
				{
					temp1 += count / 2;
					number += (temp1 % 2);
				}
				else
				{
					number = "1";
				}
			}
		}
		return number;
		
	}// ends binary
	
}// ends class
Edited by: Jaise on Feb 18, 2008 11:09 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 18 2008
Added on Feb 18 2008
6 comments
200 views