Results 1 to 17 of 17
Thread: Help! Factoring application
 11012009, 07:03 PM #1Member
 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, ifstatements, scanner class, math class, and some other stuff. Oh and this has to be done in java. Please respond as soon as possible. Thanks!
 11012009, 07:06 PM #2Member
 Join Date
 Nov 2009
 Location
 California
 Posts
 55
 Rep Power
 0
also it has to work for all numbers not just 150
 11012009, 07:09 PM #3
 11012009, 07:13 PM #4Member
 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."); }
 11012009, 07:16 PM #5Member
 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
 11012009, 07:18 PM #6Member
 Join Date
 Nov 2009
 Location
 California
 Posts
 55
 Rep Power
 0
yeah i think that might work
thanks!
 11012009, 07:31 PM #7Member
 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
 11012009, 07:35 PM #8Member
 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
 11012009, 07:35 PM #9Member
 Join Date
 Mar 2009
 Posts
 81
 Rep Power
 0
Code tags please.
 11012009, 07:41 PM #10Member
 Join Date
 Nov 2009
 Location
 California
 Posts
 55
 Rep Power
 0
what are code tags?
sorry
 11012009, 07:44 PM #11Member
 Join Date
 Mar 2009
 Posts
 81
 Rep Power
 0
Put [ CODE ] and [ /CODE ] around your code. (without the spaces)
 11012009, 07:50 PM #12Member
 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 "); } } } }
 11012009, 07:55 PM #13Member
 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; }
 11012009, 08:58 PM #14Member
 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
"X" MUST be 1 on the last iteration, or there is another prime factor
edit:Last edited by rdtindsm; 11022009 at 04:15 AM. Reason: logic error
 11022009, 02:07 AM #15Member
 Join Date
 Nov 2009
 Location
 California
 Posts
 55
 Rep Power
 0
thanks for all the hel
 11022009, 02:09 AM #16Member
 Join Date
 Nov 2009
 Location
 California
 Posts
 55
 Rep Power
 0
sorry i meant "help" not "hel"
 11022009, 02:15 AM #17
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,372
 Blog Entries
 1
 Rep Power
 20
Hi blueduiker, if you have free time please read our FAQ page. You can find lots of useful things from there.
Similar Threads

Sending J2ME application by blue tooth (by J2ME application). Very URGENT!!!
By maruffaiz in forum CLDC and MIDPReplies: 0Last Post: 04222009, 01:30 PM 
Launching an application from another application dynamically
By Java Tip in forum Java TipReplies: 0Last Post: 02162008, 09:31 PM 
Launching an application from another application using thread
By Java Tip in forum Java TipReplies: 0Last Post: 02162008, 09:29 PM 
Help, GUI Application
By Felissa in forum AWT / SwingReplies: 2Last Post: 07042007, 08:19 AM
Bookmarks