# Thread: Help! Factoring application

1. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0

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

Please enter an integer: 150

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!

2. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0
also it has to work for all numbers not just 150

3. What have you done so far? What approaches have you tried?

4. Member
Join Date
Mar 2009
Posts
81
Rep Power
0
Something like this?

Java 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.

5. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0
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

6. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0
yeah i think that might work
thanks!

7. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0
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

8. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0
Also 25 returns as 5 x
how do i have it return as 5 x 5

9. Member
Join Date
Mar 2009
Posts
81
Rep Power
0

10. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0
what are code tags?
sorry

11. Member
Join Date
Mar 2009
Posts
81
Rep Power
0
Put [ CODE ] and [ /CODE ] around your code. (without the spaces)

12. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0
code looks like this so far:
Java 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 ");
}
}
}
}```

13. Member
Join Date
Mar 2009
Posts
81
Rep Power
0
Try this as a method.

Java 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.

14. Member
Join Date
Feb 2009
Posts
92
Rep Power
0
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.

Java 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:
Last edited by rdtindsm; 11-02-2009 at 05:15 AM. Reason: logic error

15. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0
thanks for all the hel

16. Member
Join Date
Nov 2009
Location
California
Posts
55
Rep Power
0
sorry i meant "help" not "hel"

17. Hi blueduiker, if you have free time please read our FAQ page. You can find lots of useful things from there.

#### Posting Permissions

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