Java Prime Lab

Discussion in 'Mac Programming' started by Maleficum, Oct 30, 2006.

  1. Maleficum macrumors member

    Joined:
    Jun 29, 2006
    Location:
    In a Chair
    #1
    For my Computer Science II class, I kind of need a bit of help. The object of this lab is to make it print "[insert number] is prime" or "[insert number] is not prime." When I run it, I get an infinite loop, basically, that has a few numbers at the beginning and ends with a seemingly unending run of
    "
    1115125003 is not prime
    1115125003 is not prime
    1115125003 is not prime
    1115125003 is not prime
    1115125003 is not prime
    "
    I don't know what I've done wrong. Here's the code.

    Code:
    import java.lang.Math.*;
    
    class Prime
    {
    	private int number;
    	private boolean isPrime;
    
    	public Prime()
    	{
    		number=0;
    		isPrime=false;
    	}
    
    	public Prime(int num)
    	{
        number=num;
    
    
    	}
    
    	public void setPrime(int num)
    	{
       number=num;
    
    	}
    
    	public void isPrime( )
    	{
    		   for(int i=2; i<Math.sqrt(number); i++)
       {
        if(number%i==0)
        System.out.println(number+" is not prime.");
        else if(number%i!=0)
          System.out.println(number+" is prime.");
    
    
    
    }
    }
    
    	public String toString()
    	{
    		String output="";
    		
       
    
        return output;
    
    
    
    		
    	}
    }
    
    public class Lab08d
    {
    	public static void main ( String[] args )
    	{
    		Prime test = new Prime(24);
    		test.isPrime();
    		System.out.println(test);
    
    		test.setPrime(7);
    		test.isPrime();
    		System.out.println(test);
    
    		test.setPrime(2);
    		test.isPrime();
    		System.out.println(test);
    
    		test.setPrime(7334);
    		test.isPrime();
            System.out.println(test);
    
    		test.setPrime(7919);
    		test.isPrime();
    		System.out.println(test);
    
    		test.setPrime(1115125003);
    		test.isPrime();
    		System.out.println(test);
    	}
    }
     
  2. HexMonkey Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #2
    The problem is your isPrime() method. The text output is embedded within the for loop, so you'll get approximately sqrt(number) lines printed when testing if number is prime.

    For example, when testing 1115125003, it will first check if it's divisible by 2 and then print:
    1115125003 is prime​
    Then it will check for divisibility by 3 and again print:
    1115125003 is prime​
    And so on up until 33393.

    When determining primality of a number, if you find that a number divides into the number you're checking, then you immediately know it's not prime and you can stop looking. Otherwise, if the number is not divisible by any number, you have determined that the number is prime - but only after checking every number (not strictly true, you only have to check prime numbers less than sqrt(number), but for simplification we'll ignore that). The following code should work:

    Code:
    	public void isPrime( )
    	{
    		for(int i=2; i<Math.sqrt(number); i++)
    		{
    			if(number%i==0)
    			{
    				System.out.println(number+" is not prime.");
    				return;
    			}
    		}
    		System.out.println(number+" is prime.");
    	}
     

Share This Page