# Help! Factoring application

• 11-01-2009, 08:03 PM
blueduiker
Help! Factoring application
Hi everyone, I enrolled in APCS this year and am new to java. So far I have been able to do all my projects with ease however this one seems difficult. Here is what my teacher wants the code to look like:
----------------------------------------------------------------------------
Sample Output:

Factored:

150 = 2 x 3 x 5 x 5
----------------------------------------------------------------------------
Could someone please help me? Here are some rules: we have not learned/not supposed to use arrays (idk if that helps), we know switch statements, for/do/while loops, if-statements, scanner class, math class, and some other stuff. Oh and this has to be done in java. Please respond as soon as possible. Thanks!
• 11-01-2009, 08:06 PM
blueduiker
also it has to work for all numbers not just 150
• 11-01-2009, 08:09 PM
mrmatt1111
What have you done so far? What approaches have you tried?
• 11-01-2009, 08:13 PM
bubbless
Something like this?

Code:

```int number = 150; int factor = 0; for(int i = 2; i <= number; i++) {   if((number % i) == 0) {     factor = i;     break;   } } if(factor != 0) {   System.out.println("Factored: "+number+" = "+factor+" x "+number/factor); } else {   System.out.println("Not possible."); }```
You can put it in a loop until you have the final result.
• 11-01-2009, 08:16 PM
blueduiker
i tried doing an extensive number of if statements but that hasn't worked because there are an infinite number of prime numbers, so I deleted the code
• 11-01-2009, 08:18 PM
blueduiker
yeah i think that might work
thanks!
• 11-01-2009, 08:31 PM
blueduiker
hmm i need to find a way to eliminate all none prime numbers
my code looks like:

import java.util.Scanner;
import java.text.DecimalFormat;
public class factor{
public static void main (String[] args) {

//This is my declaration section
int user_input, factor;
Scanner scan = new Scanner(System.in);

//This section receives the users input
System.out.print ("Please enter an integer to be factored:");
user_input = scan.nextInt();

System.out.println ("Factored:");
System.out.print (user_input + " = ");

for (int i = 2; i < user_input; i++) {
if ((user_input % i) == 0) {
factor = i;
System.out.print (factor + " x ");
}
}
}
}

The output for 24 with this code is:
Please enter an integer to be factored: [24]
Factored:
24 = 2 x 3 x 4 x 6 x 8 x 12 x

Also how do i get rid of that extra multiplication sign
• 11-01-2009, 08:35 PM
blueduiker
Also 25 returns as 5 x
how do i have it return as 5 x 5
• 11-01-2009, 08:35 PM
bubbless
• 11-01-2009, 08:41 PM
blueduiker
what are code tags?
sorry
• 11-01-2009, 08:44 PM
bubbless
Put [ CODE ] and [ /CODE ] around your code. (without the spaces)
• 11-01-2009, 08:50 PM
blueduiker
code looks like this so far:
Code:

```import java.util.Scanner; import java.text.DecimalFormat; public class factor{ public static void main (String[] args) { //This is my declaration section int user_input, factor; Scanner scan = new Scanner(System.in); //This section receives the users input System.out.print ("Please enter an integer to be factored:"); user_input = scan.nextInt(); System.out.println ("Factored:"); System.out.print (user_input + " = "); for (int i = 2; i < user_input; i++) { if ((user_input % i) == 0) { factor = i; System.out.print (factor + " x "); } } } }```
• 11-01-2009, 08:55 PM
bubbless
Try this as a method.

Code:

```public method getNextFactor(int number) {   int factor = 0;   for (int i = 2; i < number; i++) {     if ((number % i) == 0) {       factor = i;       break;     }   }   return factor; }```
Use this method inside a loop to find all the factors.
• 11-01-2009, 09:58 PM
rdtindsm
OP seems to have the right idea on displaying his ouput. Will leave it to him to figure that out.

What I have noticed is that he is incrementing his factor after he verifies that it is a factor. This will not be correct.

Consider a number like 40 (2 x 2 x 2 x5);
You will find 2, increment to 3 (not a factor), then increment to 4 which is a factor. But 4 is 2 x 2. You need to loop with the value 2 until you find that it is no longer a factor. Don't worry about the number being prime. You will find them as you progress in the algorithm.

http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

The sieve of eratosthenes merely finds the primes, but the reference will help you understand what you are doing.

Code:

```Factor(X) n=2 while (not end condition)   if (n is a factor of X) {   add n to factor list   X = X / n   } // if   while(n is a factor of X {       add n to factor list       X = X / n   }  //while   if((  X  equals 1) ){         end condition = true   }  //if   increment n } //while end Factor```
edit: moved increment statement after if test, removed "and (n divides X) evenly.
"X" MUST be 1 on the last iteration, or there is another prime factor

edit:
• 11-02-2009, 03:07 AM
blueduiker
thanks for all the hel
• 11-02-2009, 03:09 AM
blueduiker
sorry i meant "help" not "hel"
• 11-02-2009, 03:15 AM
Eranga
Hi blueduiker, if you have free time please read our FAQ page. You can find lots of useful things from there.