Results 1 to 11 of 11
  1. #1
    grrttrdsll is offline Member
    Join Date
    May 2013
    Posts
    12
    Rep Power
    0

    Default 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;
    }

    }
    }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    grrttrdsll is offline Member
    Join Date
    May 2013
    Posts
    12
    Rep Power
    0

    Default 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).

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default 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:


    Java 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.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default 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.

    Java 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Problem on project

    I believe you have syntax problems. Did you try this yourself??

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    grrttrdsll is offline Member
    Join Date
    May 2013
    Posts
    12
    Rep Power
    0

    Default 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.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default Re: Problem on project

    Okay, here is some pseudo code.

    Java 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
    Last edited by jim829; 05-29-2013 at 01:24 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    grrttrdsll is offline Member
    Join Date
    May 2013
    Posts
    12
    Rep Power
    0

    Default 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.

    Java 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?

  10. #10
    grrttrdsll is offline Member
    Join Date
    May 2013
    Posts
    12
    Rep Power
    0

    Default Re: Problem on project

    I think I ended up solving the problem. I ended up with this code

    Java 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?

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,382
    Rep Power
    5

    Default 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.

    Java 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
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Problem with rounding up in a school project
    By OlivierSillegue in forum New To Java
    Replies: 10
    Last Post: 02-13-2012, 03:10 AM
  2. java project problem
    By mr_byte31 in forum New To Java
    Replies: 2
    Last Post: 11-13-2010, 08:45 PM
  3. project problem
    By arsenal4ever_11 in forum NetBeans
    Replies: 4
    Last Post: 05-29-2010, 10:47 AM
  4. Project: CarProject problem
    By Rhez in forum New To Java
    Replies: 6
    Last Post: 10-15-2009, 04:51 AM
  5. my project problem??help please
    By sheckoo in forum New To Java
    Replies: 4
    Last Post: 12-06-2008, 01:14 AM

Posting Permissions

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