Results 1 to 16 of 16
 10212009, 09:56 PM #1Member
 Join Date
 Oct 2009
 Posts
 13
 Rep Power
 0
Returning the Greatest Prime Factor
Hi, everyone, I am using Eclipse in my Java class to write programs that the teacher requests. In this particular assignment, we were told to modify a program we already wrote to tell not only whether or not something was prime, but also tell us what the greatest prime factor of a particular int is. Unfortunately, my original program almost worked perfectly, but after making it robust it crashed every time I entered numbers like 24, 42, <10, etc. So he suggested I rewrite the part which determined the greatest prime factor of a number, and here is my existing program:
Java Code:import java.util.*; public class PrimeNum { public static void main(String[] args) { System.out.println("This program will allow you to enter a positive integer and then"); System.out.println("test it to see if it is prime or not. Entering a negative number"); System.out.println("will close down the application."); System.out.println(); while (true) { boolean prime = (true); int num = 0; int factor = 0; int a = 2; String continuation; Scanner choices = new Scanner (System.in); System.out.println("Enter a positive integer to test if it is prime:"); Scanner a_number = new Scanner (System.in); while (true) { try { num = a_number.nextInt(); } catch (InputMismatchException e) { System.out.println( e + " I'm sorry, I don't understand. Please enter a positive integer:" ); a_number.nextLine(); continue; } int i = 2; if (num < 0){ System.out.println("Goodbye!"); System.exit(1); } while (i < ((int)Math.sqrt(num)+1)) { if (num % i != 0){ i++; continue; } if (num % i == 0){ prime = (false); break; } break; } for (a = 2; num >= 2; a++) { while (factor % a == 0){ factor = (factor / a); if (factor == 1){ factor = num; break; } } if (a > factor) { break; } else a++; continue; } if (prime) { System.out.println("The number " + num + " is prime."); System.out.println(""); System.out.println("Would you like to continue?"); while (true) { continuation = choices.next(); if (continuation.charAt(0) == 'N'  continuation.charAt(0) == 'n'  continuation.equals("No")  continuation.equals("no")) { System.out.println("Goodbye!"); System.exit(1); } else break; } } else if (!prime) { System.out.println("The number " + num + " is not prime."); System.out.println("The greatest prime factor of " + num + " is " + factor + "."); System.out.println(""); System.out.println("Would you like to continue?"); while (true) { continuation = choices.next(); if (continuation.charAt(0) == 'N'  continuation.charAt(0) == 'n'  continuation.equals("No")  continuation.equals("no")) { System.out.println("Goodbye!"); System.exit(1); } break; } break; } break; } continue; } } } //End of class PrimeNum
The other problem is that the current method for finding the GPF has not actually been tested. So if it does work, I am okay as it is, but if it doesn't, then it might be helpful to know a better method to find the correct GPF of an int. Any help would be much appreciated.
Thanks
 10212009, 10:21 PM #2Member
 Join Date
 Oct 2009
 Posts
 63
 Rep Power
 0
which loop is it getting stuck in?
 10212009, 10:23 PM #3Member
 Join Date
 Oct 2009
 Posts
 63
 Rep Power
 0
Here is your problem
Java Code:while (true) {
 10212009, 10:49 PM #4Member
 Join Date
 Oct 2009
 Posts
 13
 Rep Power
 0
Which problem is that related to? How can I remove that to make the rest of the program work? And which "while (true) {"?
 10222009, 01:01 AM #5Member
 Join Date
 Oct 2009
 Posts
 25
 Rep Power
 0
I suggest that you clean up your code a bit. There is no reason to have so many loops that never end with all those break statements. It will make your program easier to debug. Another thing you could do is to refactor code out into their own methods.
 10222009, 02:50 AM #6Member
 Join Date
 Oct 2009
 Posts
 13
 Rep Power
 0
You see, the thing is, I would. But in our Java class, we've learned about up to loops, and that is about all I know how to do other than a bit of math, boolean, etc. Plus, the assignment tells us to use loops to make this thing. I am not great with loops, so it really isn't that easy for me to do this.
Right now, the instructor wants us to make it so that the program will continue to loop until the person either types a negative number, or if the person says that they don't want to continue at the end of the program. That will make it exit. The problem is, the only good way I could find to do that is wrap up the entire program in a "while (true) {" loop. The teacher hasn't taught us anything like that, and it's rather hard to add more math to a math program without some sort of error.
This is supposed to be simple math, which seems to make the program not understand how to find the single greatest prime factor of a number. I for one, can do most of this mentally without scratch paper. But this exercise is really quite requiring of a particular math which is almost uncalculatorable.
So yeah, I'm stumped. On top of that, there is an endless loop, which, seems to be affecting not only me, but a few other students in my class.
 10222009, 08:26 AM #7Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 9
 10222009, 04:09 PM #8Member
 Join Date
 Oct 2009
 Posts
 63
 Rep Power
 0
Java Code:import java.util.*; public class PrimeNum { public static void main(String[] args) { System.out.println("This program will allow you to enter a positive integer and then"); System.out.println("test it to see if it is prime or not. Entering a negative number"); System.out.println("will close down the application."); System.out.println(); while (true) { boolean prime = (true); int num = 0; int factor = 0; int a = 2; String continuation; Scanner choices = new Scanner (System.in); System.out.println("Enter a positive integer to test if it is prime:"); Scanner a_number = new Scanner (System.in);
Try something like this for the first while(true) part of your program. Then try to do something similar for the next while(true) and let us know where you get stuck.
Java Code:import java.util.*; public class PrimeNum { public static void main(String[] args) { System.out.println("This program will allow you to enter a positive integer and then"); System.out.println("test it to see if it is prime or not. Entering a negative number"); System.out.println("will close down the application."); System.out.println(); boolean prime = (true); int num = 0; int factor = 0; int a = 2; String continuation; [COLOR="red"]boolean positive = true;[/COLOR] [COLOR="red"]while (positive) { <this says "While positive is true, go in the loop"[/COLOR] Scanner choices = new Scanner (System.in); System.out.println("Enter a positive integer to test if it is prime:"); [COLOR="red"]String a_number [/COLOR]= new Scanner (System.in); [COLOR="red"] if(a_number < 0) { positive = false; }[/COLOR]
 10232009, 09:19 PM #9Member
 Join Date
 Oct 2009
 Posts
 13
 Rep Power
 0
Okay, so I am modifying the code to follow the pattern which you wrote. However, somewhere in your version the
Java Code:Scanner choices = new Scanner (System.in)
Java Code:Scanner a_number = new Scanner (System.in)
What was that supposed to accomplish?
 10232009, 09:32 PM #10Member
 Join Date
 Oct 2009
 Posts
 13
 Rep Power
 0
My infinite loop is only caused when I enter a number into the Scanner, and the number is greater than 1. After that, it leaves the program running, Scanner still operational, and no matter what I enter into the Scanner, it continues to loop, giving me only the option to enter something into the Scanner. No text is printed out of the System, just input. And it is only with integers greater than 1.
It will list 0 & 1 as prime, and will prompt me to continue, and loops through just fine, but it is really only a problem that seems to be coming from positive integers greater than 1. That is essentially a problem, because positive integers greater than 1 are the only premise for this thing basically. You can't have GPF's of a negative number, it just doesn't work.
Here is my modified code. It's really not much different, and it's working exactly the same way as it did before the changes.
Java Code:import java.util.*; public class PrimeNum { public static void main(String[] args) { System.out.println("This program will allow you to enter a positive integer and then"); System.out.println("test it to see if it is prime or not. Entering a negative number"); System.out.println("will close down the application."); System.out.println(); boolean prime = (true); int num = 0; int factor = 0; int a = 2; String continuation; boolean positive = true; Scanner choices = new Scanner (System.in); System.out.println("Enter a positive integer to test if it is prime:"); Scanner a_number = new Scanner (System.in); try { num = a_number.nextInt(); } catch (InputMismatchException e) { System.out.println( e + " I'm sorry, I don't understand. Please enter a positive integer:" ); a_number.nextLine(); continue; } int i = 2; if (num < 0){ positive = false; } if (!positive) { System.out.println("Goodbye."); System.exit(1); } while (i < ((int)Math.sqrt(num)+1)) { if (num % i != 0){ i++; continue; } if (num % i == 0){ prime = (false); break; } break; } for (a = 2; num >= 2; a++) { //Beginning of the GPF solving loop while (factor % a == 0){ factor = (factor / a); if (factor == 1){ factor = num; break; }//End if } if (a > factor) { break; } else a++; continue; }//End for if (prime) { System.out.println("The number " + num + " is prime."); System.out.println(""); System.out.println("Would you like to continue?"); while (positive) { continuation = choices.next(); if (continuation.charAt(0) == 'N'  continuation.charAt(0) == 'n'  continuation.equals("No")  continuation.equals("no")) { System.out.println("Goodbye!"); System.exit(1); } else break; } } else if (!prime) { System.out.println("The number " + num + " is not prime."); System.out.println("The greatest prime factor of " + num + " is " + factor + "."); System.out.println(""); System.out.println("Would you like to continue?"); while (positive) { continuation = choices.next(); if (continuation.charAt(0) == 'N'  continuation.charAt(0) == 'n'  continuation.equals("No")  continuation.equals("no")) { System.out.println("Goodbye!"); System.exit(1); } break; } break; } continue; } } } //End of class PrimeNum
 10232009, 09:34 PM #11Member
 Join Date
 Oct 2009
 Posts
 63
 Rep Power
 0
I meant for this
String a_number = new Scanner (System.in);
String a_number = choices.nextInt
 10232009, 09:40 PM #12Member
 Join Date
 Oct 2009
 Posts
 13
 Rep Power
 0
I now have a syntax error that says I cannot convert from String to int. Is there a particular something I should change this to?
I changed it toJava Code:num = choices.nextInt();
Last edited by BJ1110; 10232009 at 09:44 PM.
 10232009, 09:52 PM #13Member
 Join Date
 Oct 2009
 Posts
 63
 Rep Power
 0
Throw a bunch of println's inside some of your loops to determine where in your program the infinite loop is.
 10232009, 09:53 PM #14Member
 Join Date
 Oct 2009
 Posts
 63
 Rep Power
 0
My bad about this
Java Code:String a_number = choices.nextInt
 10232009, 09:54 PM #15Member
 Join Date
 Oct 2009
 Posts
 13
 Rep Power
 0
That's exactly what my Java teacher keeps saying. I guess I'll give it a try.
 10232009, 10:06 PM #16Member
 Join Date
 Oct 2009
 Posts
 63
 Rep Power
 0
It's one way to debug code. This is what I always do. I put a line like this
System.out.println("Here?");
right inside of a loop. If you run your program and that prints, you know it got to that point in your program. Now take that line out and put it further in the program like in another loop. Continue this until your program never prints that line, or keeps printing it. That should help you find the infinite loop.
Similar Threads

Finding Largest Prime Factor
By perito in forum New To JavaReplies: 7Last Post: 11082010, 09:25 PM 
Prime Number  System print all the prime numbers ...
By pinkdreammsss in forum New To JavaReplies: 20Last Post: 04262009, 01:50 AM 
find the greatest and lowest number in 2D array
By le_albina@hotmail.com in forum New To JavaReplies: 2Last Post: 03302009, 11:09 PM 
Prime numbers
By tercius in forum New To JavaReplies: 3Last Post: 05042008, 06:05 AM 
Prime numbers
By gapper in forum New To JavaReplies: 3Last Post: 02072008, 11:09 AM
Bookmarks