# Returning the Greatest Prime Factor

• 10-21-2009, 09:56 PM
BJ1110
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:

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```
For some reason, it gets stuck in an infinte loop, and that is the first problem I need help solving.

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
• 10-21-2009, 10:21 PM
toymachiner62
which loop is it getting stuck in?
• 10-21-2009, 10:23 PM
toymachiner62
Code:

`while (true) {`
Since you're not passing a variable or a condition this will never change.
• 10-21-2009, 10:49 PM
BJ1110
Which problem is that related to? How can I remove that to make the rest of the program work? And which "while (true) {"?
• 10-22-2009, 01:01 AM
literallyjer
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.
• 10-22-2009, 02:50 AM
BJ1110
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.
• 10-22-2009, 08:26 AM
r035198x
• 10-22-2009, 04:09 PM
toymachiner62
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);```
well for this part of your code, you are indefinitely asking the user for a number since you are asking them inside of your infinite loop.

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.

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]```
• 10-23-2009, 09:19 PM
BJ1110
Okay, so I am modifying the code to follow the pattern which you wrote. However, somewhere in your version the

Code:

`Scanner choices = new Scanner (System.in)`
was lost in the code changing. Also the second Scanner on the list,

Code:

`Scanner a_number = new Scanner (System.in)`
was changed to be a String. However, this brought up a syntax error because I am not able to make a String equal to a Scanner.

What was that supposed to accomplish?
• 10-23-2009, 09:32 PM
BJ1110
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.

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```
• 10-23-2009, 09:34 PM
toymachiner62
I meant for this
Quote:

String a_number = new Scanner (System.in);
to be this
Quote:

String a_number = choices.nextInt
That way it'll set a_number to the int that the user enters.
• 10-23-2009, 09:40 PM
BJ1110
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 to
Code:

` num = choices.nextInt();`
and it's back to what it was just doing, but at least there are no errors.
• 10-23-2009, 09:52 PM
toymachiner62
Throw a bunch of println's inside some of your loops to determine where in your program the infinite loop is.
• 10-23-2009, 09:53 PM
toymachiner62
Code:

`String a_number = choices.nextInt`
I mean to put int a_number but you figured it out.
• 10-23-2009, 09:54 PM
BJ1110
That's exactly what my Java teacher keeps saying. I guess I'll give it a try.
• 10-23-2009, 10:06 PM
toymachiner62
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.