1. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## 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;

int newDivisible = getdivisible(newnumber);
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
} // 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. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## Re: Prime Factorization

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

3. Senior Member
Join Date
Nov 2010
Posts
210
Rep Power
7

## 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. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## Re: Prime Factorization

Originally Posted by Iron Lion
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. Senior Member
Join Date
Nov 2010
Posts
210
Rep Power
7

## 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. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## 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;

int newDivisible = getdivisible(newnumber);
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
} // 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. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## 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);

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
} // 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. Member
Join Date
Sep 2011
Posts
56
Rep Power
0

## 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

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
} // 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

#### Posting Permissions

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