# Problem on project

Printable View

• 05-29-2013, 12:00 AM
grrttrdsll
Problem on project
For a class I'm taking on java (basic level college course) I'm having some trouble on the homework.

We are writing a program that evaluates whether or not a number is prime or not. I have this -semi- working, but for the numbers 2,9, and 93 I cannot get the program to read the correct answer.

I was wondering if anyone could point out whatever I'm clearly missing here because I'm stumped.

here is the code:

import java.util.Scanner;
public class Assignment3 {
public static void main (String[]args){

Scanner in=new Scanner (System.in);
int num;
System.out.print("Please enter a positive integer:");
num=in.nextInt();

for (int i=2; i<num;i++){
//for (i=num-1;i>1;i--){

if (num%i==0){
//composite
System.out.print(num+" Composite ");
System.out.print(num/i);
break;
}

else if (num%i>0){
//prime
System.out.print(num+ " is prime");
break;
}

}
}
}
• 05-29-2013, 12:13 AM
jim829
Re: Problem on project
So you are using modularity to check to see if any of the divisors from 2 to the number-1 evenly divide the number. The problem is the algorithm. Only one value from 2 to inum-1 needs to divide the number to determine if it is composite. But all the numbers from 2 to inum-1 need to return a non-zero value to determine primality.

Also, your loop for 2 will not run because it starts at 2 and fails the conditional when the input value is 2.

Regards,
Jim
• 05-29-2013, 12:17 AM
grrttrdsll
Re: Problem on project
What does that translate to in actual code? I'm a total novice (5th day of class) and what your saying only makes vague sense if anything to me...
It's much easier for me to understand things in terms of reverse engineering (if that isn't too much hassle for you).
• 05-29-2013, 12:38 AM
monkeyjr97
Re: Problem on project
what you need to do is:

1, get the number from a user or hard code it:(call this n)
2, start at 2 and see if the number can be divided by 2 (n%2 ==0) if it cant so (n%2 !=0) then try the next number:
2b, continue step to increasing the division (call this i) untill you reach the original number, if all these i values return a remainder then the number is prime:

Code:

int n = in.nextInt();

for(int i=2;i<n;i++) { //check every number between 2 and your number
if(n%i==0)
System.out.println("NOT PRIME, " + n + "Divides by: " + i );
}

else {
System.out.println("" + n + "  is prime");
}

NOTE: This can be improved massively.
• 05-29-2013, 12:38 AM
jim829
Re: Problem on project
Something I forgot to mention is to place you code in code tages [CODE][/CODE] so you code if formatted. I have done it with your supplied code.

Code:

import java.util.Scanner;
public class Assignment3 {
public static void main (String[]args){

Scanner in=new Scanner (System.in);
int num;
System.out.print("Please enter a positive integer:");
num=in.nextInt();

for (int i=2; i<num;i++){
//for (i=num-1;i>1;i--){

if (num%i==0){
//composite
System.out.print(num+" Composite ");
System.out.print(num/i);
break;
}

else if (num%i>0){
//prime
System.out.print(num+ " is prime");
break;
}

}
}
}

The problem is your test for primality (else if statement). What happens if your number is 9 and i = 2? Then 9%2 == 1 so you tag it as prime and break out of the loop. An easier way to approach this problem is check to see if it is composite. If so, set a flag of some sort. When the loop finishes either naturally or via a break, check the flag to determine if it was a prime or composite.

Also, it is customary to *not* write code to solve problems for posters in this forum. Most will help you diagnose the problem and may offer code snippets or complete apps to demonstrate a very small part of the overall submitted code.

Regards,
Jim
• 05-29-2013, 12:41 AM
jim829
Re: Problem on project
I believe you have syntax problems. Did you try this yourself??

Regards,
Jim
• 05-29-2013, 01:01 AM
grrttrdsll
Re: Problem on project
I'm totally clueless how I'm supposed to write this loop and flag. I'm still only vaguely understanding what you are saying. I totally understand the whole "dont write out the whole code for them" part though.

I need more of an explanation of how that happens and how I change it to meet that.
• 05-29-2013, 01:19 AM
jim829
Re: Problem on project
Okay, here is some pseudo code.

Code:

int prime = 0;
For div = 2 to numb-1
if  (div divides numb) {
prime = 1;
break;
}
}

At this point, prime is either 0 or 1 based on the loop and test.

Regards,
Jim
• 05-29-2013, 01:26 AM
grrttrdsll
Re: Problem on project
I guess a better question might be to ask how to properly flag a number as prime now. I broke down my code more and got the composites to read out correctly, but I still haven't figured out how to read out the primes correctly.

Code:

import java.util.Scanner;
public class Assignment3 {
public static void main (String[]args){

Scanner in=new Scanner (System.in);
int num,i;
System.out.print("Please enter a positive integer:");
num=in.nextInt();

for(i=num-1;i>1;i--){
if(num%i==0){
System.out.print(num+" Composite ");
System.out.print(i);
break;

}
}
}
}

What is the best way to differentiate the primes and composites now?
• 05-29-2013, 01:48 AM
grrttrdsll
Re: Problem on project
I think I ended up solving the problem. I ended up with this code

Code:

import java.util.Scanner;
public class Assignment3 {
public static void main (String[]args){

Scanner in=new Scanner (System.in);
int num,i;
System.out.print("Please enter a positive integer:");
num=in.nextInt();

if(num==2){
System.out.print("prime");
}

for(i=num-1;i>1;i--){
if(num%i==0){
System.out.print(num+" Composite ");
System.out.print(i);
return;

}

}

for (i=num-1;i>1;i--){
if(num%i==1){
System.out.print("prime");
}

}
}
}

Any thoughts?
• 05-29-2013, 02:37 AM
jim829
Re: Problem on project
Well, you're almost there. Your previous effort was the better one. Simply iterate thru the loop, checking to see if the number is composite. As soon as it is, set a flag. Then when you exit the loop, use the flag to determine whether the number is composite or not. Try and go thru this to understand the changes I made.

Code:

Scanner in=new Scanner (System.in);
int num,i;
int isPrime = 0;  // 0 means prime, 1 means composite
System.out.print("Please enter a positive integer:");
num=in.nextInt();

for(i=2; i < num; i++){ // back the way it was
if(num%i==0){
isPrime = 1;
//          System.out.print(num+" Composite ");
//          System.out.print(i);
break;

}
}
//  The idea is that if you go thru the loop without finding a divisor, then the number must be prime, so isPrime remains 0.
if (isPrime == 0) {
// the number is prime
} else {
// the number is composite
}

Regards,
Jim