Thread: Returning the Greatest Prime Factor

1. Member
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 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;
}
}
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

2. Member
Join Date
Oct 2009
Posts
63
Rep Power
0
which loop is it getting stuck in?

3. Member
Join Date
Oct 2009
Posts
63
Rep Power
0
Java Code:
`while (true) {`
Since you're not passing a variable or a condition this will never change.

4. Member
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) {"?

5. Member
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.

6. Member
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.

7. Senior Member
Join Date
Aug 2009
Posts
2,388
Rep Power
13

8. Member
Join Date
Oct 2009
Posts
63
Rep Power
0
Java Code:
```import java.util.*;
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.

Java Code:
```import java.util.*;
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]```

9. Member
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)`
was lost in the code changing. Also the second Scanner on the list,

Java 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. Member
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 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;
}
}

11. Member
Join Date
Oct 2009
Posts
63
Rep Power
0
I meant for this
String a_number = new Scanner (System.in);
to be this
String a_number = choices.nextInt
That way it'll set a_number to the int that the user enters.

12. Member
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 to
Java Code:
` num = choices.nextInt();`
and it's back to what it was just doing, but at least there are no errors.
Last edited by BJ1110; 10-23-2009 at 09:44 PM.

13. Member
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.

14. Member
Join Date
Oct 2009
Posts
63
Rep Power
0
Java Code:
`String a_number = choices.nextInt`
I mean to put int a_number but you figured it out.

15. Member
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.

16. Member
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.

Posting Permissions

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