Results 1 to 17 of 17
Thread: Help! Factoring application
- 11-01-2009, 07:03 PM #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!
- 11-01-2009, 07:06 PM #2
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
also it has to work for all numbers not just 150
- 11-01-2009, 07:09 PM #3
- 11-01-2009, 07:13 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
Something like this?
You can put it in a loop until you have the final result.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."); }
- 11-01-2009, 07:16 PM #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
- 11-01-2009, 07:18 PM #6
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
yeah i think that might work
thanks!
- 11-01-2009, 07:31 PM #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
- 11-01-2009, 07:35 PM #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
- 11-01-2009, 07:35 PM #9
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
Code tags please.
- 11-01-2009, 07:41 PM #10
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
what are code tags?
sorry
- 11-01-2009, 07:44 PM #11
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
Put [ CODE ] and [ /CODE ] around your code. (without the spaces)
- 11-01-2009, 07:50 PM #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 "); } } } }
- 11-01-2009, 07:55 PM #13
Member
- Join Date
- Mar 2009
- Posts
- 81
- Rep Power
- 0
Try this as a method.
Use this method inside a loop to find all the factors.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; }
- 11-01-2009, 08:58 PM #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.
edit: moved increment statement after if test, removed "and (n divides X) evenly.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; 11-02-2009 at 04:15 AM. Reason: logic error
- 11-02-2009, 02:07 AM #15
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
thanks for all the hel
- 11-02-2009, 02:09 AM #16
Member
- Join Date
- Nov 2009
- Location
- California
- Posts
- 55
- Rep Power
- 0
sorry i meant "help" not "hel"
- 11-02-2009, 02:15 AM #17
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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: 04-22-2009, 01:30 PM -
Launching an application from another application dynamically
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:31 PM -
Launching an application from another application using thread
By Java Tip in forum Java TipReplies: 0Last Post: 02-16-2008, 09:29 PM -
Help, GUI Application
By Felissa in forum AWT / SwingReplies: 2Last Post: 07-04-2007, 08:19 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks