Results 1 to 8 of 8
  1. #1
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Prime Factorization

    I'm writing a program, to get the prime factorization of the given number.
    Thanks to iron lion my code is a easier to understand but... it doesn't work :/
    if number = 24
    it just returns 2

    Java Code:
    
    public class Prime_Factorization {
         
        public static void main (String args[]){
     
            int number = 28;        
            int divisible = getdivisible(number);
            int newnumber = number / divisible;
             
            while(primeNumberChecker(number) == false){
                int newDivisible = getdivisible(newnumber); 
                if(primeNumberChecker(newDivisible) == true){
                    System.out.println(newDivisible);
                    break;
                } //end if
                else
                {
                     newDivisible = getdivisible(newDivisible);
                }
                number = newDivisible;
            } //End while
             
         
     
         
        } //End main
             
    //***********************************DIVISIBLE FINDER*************************************************
            private static int getdivisible(int number) {
                int toReturn = 0;
                for(int i=2; i<=(number -1 ); i++){
                    if (number % i == 0){
                        toReturn = i;
                    break;
                    } //end if statement
                } // end for statement
                return toReturn;
        } // End Method
     
             
             
    //********************************PRIME CHECKER*******************************************************
            private static boolean primeNumberChecker(int input) {  //The Prime Number Checker      
        boolean isPrime =  true;
         isPrime = true;
        for(int i=2; i<=(input-1); i++){
            if (input % i == 0){
            isPrime = false;
        break;
            } //end if statement
        }   //end for statement
        //System.out.println("DEBUG @PrimeChecker: Number " + input + " is prime = " + isPrime);
        return isPrime;
    }//End method
             
             
             
    } //End Class
    Last edited by skaterboy987; 10-27-2011 at 12:36 AM.

  2. #2
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Prime Factorization

    PS: it only works if the prime factorization is exactly 3 digits

  3. #3
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Prime Factorization

    The way I'd do it is (in pseudocode):

    Code:
    primes[] = array of prime numbers
    num = number to get factors of
    while num is not prime
        for each number, p, in primes[]
            if num % p is 0
                print p + "*"
                divide num by p
                continue looping
            end if
        end for
    end while
    print num

  4. #4
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Prime Factorization

    Quote Originally Posted by Iron Lion View Post
    The way I'd do it is (in pseudocode):

    Code:
    primes[] = array of prime numbers
    num = number to get factors of
    while num is not prime
        for each number, p, in primes[]
            if num % p is 0
                print p + "*"
                divide num by p
                continue looping
            end if
        end for
    end while
    print num
    thanks for the response but what does p represent in:
    for each number, p, in primes[]

  5. #5
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    4

    Default Re: Prime Factorization

    Means whatever the value of that element of the array is.

    To come out of pseudocode and back into Java for a moment:

    Java Code:
    for (int p : primes) {
       ...
    }

  6. #6
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Prime Factorization

    This should work.... But it doesn't :( it just returns 2, :(
    Java Code:
    public class Prime_Factorization {
    	
    	public static void main (String args[]){
    
            int number = 28;        
    		int divisible = getdivisible(number);
    		int newnumber = number / divisible;
    		
    		while(primeNumberChecker(number) == false){
    			int newDivisible = getdivisible(newnumber);	
    			if(primeNumberChecker(newDivisible) == true){
    				System.out.println(newDivisible);
    				break;
    			} //end if
    			else
    			{
    				 newDivisible = getdivisible(newDivisible);
    			}
    			number = newDivisible;
    		} //End while
    		
    	
    
    	
    	} //End main
    		
    //***********************************DIVISIBLE FINDER*************************************************
    		private static int getdivisible(int number) {
    			int toReturn = 0;
    			for(int i=2; i<=(number -1 ); i++){
    				if (number % i == 0){
    					toReturn = i;
    				break;
    				} //end if statement
    			} // end for statement
    			return toReturn;
    	} // End Method
    
    		
    		
    //********************************PRIME CHECKER*******************************************************
    		private static boolean primeNumberChecker(int input) {  //The Prime Number Checker		
    	boolean isPrime =  true;
    	 isPrime = true;
    	for(int i=2; i<=(input-1); i++){
    		if (input % i == 0){
    		isPrime = false;
    	break;
    		} //end if statement
    	}	//end for statement
    	//System.out.println("DEBUG @PrimeChecker: Number " + input + " is prime = " + isPrime);
    	return isPrime;
    }//End method
    		
    		
    		
    } //End Class
    Last edited by skaterboy987; 10-27-2011 at 12:26 AM.

  7. #7
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Prime Factorization

    I believe this is an improvement, it's just the output is wrong sometimes....
    Java Code:
    public class Prime_Factorization {
    	
    	public static void main (String args[]){
    
            int number = 28;        
    
    		
    		
    		
    		while(primeNumberChecker(number) == false){ //While number is not prime
    			int getdivisible = getdivisible(number);	
    			int divisible = getdivisible;
    			int divisible2 = number / getdivisible; //Second divisible ex 2 / 28 = 14, returns 14
    			if(primeNumberChecker(divisible) == true){ //If divisible is prime
    				System.out.println(divisible);
    				
    			
    			if(primeNumberChecker(divisible2) == true){
    				System.out.println(divisible);
    			}
    			}
    		number = divisible2;
    		} //End while
    		
    	
    
    	
    	} //End main
    		
    //***********************************DIVISIBLE FINDER*************************************************
    		private static int getdivisible(int number) {
    			int toReturn = 0;
    			for(int i=2; i<=(number -1 ); i++){
    				if (number % i == 0){
    					toReturn = i;
    				break;
    				} //end if statement
    			} // end for statement
    			return toReturn;
    	} // End Method
    
    		
    		
    //********************************PRIME CHECKER*******************************************************
    		private static boolean primeNumberChecker(int input) {  //The Prime Number Checker		
    	boolean isPrime =  true;
    	 isPrime = true;
    	for(int i=2; i<=(input-1); i++){
    		if (input % i == 0){
    		isPrime = false;
    	break;
    		} //end if statement
    	}	//end for statement
    	//System.out.println("DEBUG @PrimeChecker: Number " + input + " is prime = " + isPrime);
    	return isPrime;
    }//End method
    		
    		
    		
    } //End Class

  8. #8
    skaterboy987 is offline Member
    Join Date
    Sep 2011
    Posts
    56
    Rep Power
    0

    Default Re: Prime Factorization

    YAY
    I got it to work, I just needed a break for when the second number was prime :)
    This does work right? If you can find a situation where it doesn't please tell me!

    Java Code:
    public class Prime_Factorization {
    	
    	public static void main (String args[]){
    
            int number = 200;        
     
    		
    		
    		while(primeNumberChecker(number) == false){ //While number is not prime
    			int getdivisible = getdivisible(number);	
    			int divisible = getdivisible;
    			int divisible2 = number / getdivisible; //Second divisible ex 2 / 28 = 14, returns 14
    			if(primeNumberChecker(divisible) == true){ //If divisible is prime
    				System.out.println(divisible);
    				} //end if
    			
    				else
    				{
    					number = divisible;
    				} //end else
    				
    			if(primeNumberChecker(divisible2) == true){
    				System.out.println(divisible2);
    				break;
    			} //end if
    			else
    			{	
    				number = divisible2 ;
    			} //end else
    			}//end while
    			} //end main
    
    	
    
    	
    
    		
    //***********************************DIVISIBLE FINDER*************************************************
    		private static int getdivisible(int number) {
    			int toReturn = 0;
    			for(int i=2; i<=(number -1 ); i++){
    				if (number % i == 0){
    					toReturn = i;
    				break;
    				} //end if statement
    			} // end for statement
    			return toReturn;
    	} // End Method
    
    		
    		
    //********************************PRIME CHECKER*******************************************************
    		private static boolean primeNumberChecker(int input) {  //The Prime Number Checker		
    	boolean isPrime =  true;
    	 isPrime = true;
    	for(int i=2; i<=(input-1); i++){
    		if (input % i == 0){
    		isPrime = false;
    	break;
    		} //end if statement
    	}	//end for statement
    	return isPrime;
    }//End method
    		
    		
    		
    } //End Class
    Yeah, it's worked for everything i've tried

Similar Threads

  1. displaying prime number
    By eng_hyzoom in forum New To Java
    Replies: 1
    Last Post: 11-26-2010, 11:21 AM
  2. greatest prime factor
    By java_prgr in forum New To Java
    Replies: 2
    Last Post: 07-23-2010, 08:28 PM
  3. Prime Number - System print all the prime numbers ...
    By pinkdreammsss in forum New To Java
    Replies: 20
    Last Post: 04-26-2009, 01:50 AM
  4. Prime numbers
    By tercius in forum New To Java
    Replies: 3
    Last Post: 05-04-2008, 06:05 AM
  5. Prime numbers
    By gapper in forum New To Java
    Replies: 3
    Last Post: 02-07-2008, 10:09 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
  •