Results 1 to 4 of 4

Thread: Prime Number Help

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Location
    Okinawa, Japan
    Posts
    6

    Prime Number Help

    I'm working on a homework assignment with the following requirement:

    Write a Java program that meets the following requirements:

    Declare a method to determine whether an integer is a prime number

    Use the following method declarations: public static Boolean isPrime (int num)

    An integer greater than 1 is a prime number if its only divisor is 1 or itself. For example, isPrime (11) returns true, and isPrime (9) returns false.

    Us the isPrime method to find the first thousand prime numbers and display every ten prime numbers in a row, as follows:

    2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 61 73 79 83 89 97 … …


    Important Notes: The input and output must use JOptionPane dialog and display boxes.
    Here is what I have so far:
    Code:
    import javax.swing.JOptionPane;
    
    public class PrimeNumber {
    
    	public static boolean isPrime(int num) {
    		if (num <  2) return false;
    		if (num == 2) return true;
    		int n = (int) Math.sqrt(num);
    		for (int i = 3; i < n; i += 2) {
    			if (num%i == 0) return false;
    		}
    		return true;
    }
    	public void main(String[] args) {
    		int[] primes = new int[1000];	
    		primes[0] = 2;				
    		int count = 1;
    		int num = 3;
    		// Find the first 1000 primes
    		while (count<1000) {
    			if (isPrime(num)) {
    				primes[count] = num; 
    				count++;
    			}
    			num = num + 2;
    		}
    		String output = (primes[i] + "\t");
    		 		
    			// Display the result
    		JOptionPane.showMessageDialog(null, output, 
    			"Assignment 5.1 Output", JOptionPane.INFORMATION_MESSAGE);
    		
    		System.exit(0);
    	}
    }
    I keep getting this error in JCreator Lite:
    C:\Users\Craig\Documents\Downloads\PrimeNumber.java:57: cannot find symbol
    symbol : variable i
    location: class PrimeNumber
    .........String output = (primes[i] + "\t");
    ........................................^
    1 error

    Process completed.
    What am I doing wrong? Everything else seems to be going okay and the examples I was given in my course seem to line up.

    Thanks for any help!

  2. #2

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Location
    Okinawa, Japan
    Posts
    6

    Re: Prime Number Help

    I've made the following updates:
    Code:
    import javax.swing.JOptionPane;
    
    public class PrimeNumber {
    	// Main Method
    	public static boolean isPrime(int num) {
    		
    		int[] numbers = new int[0];
    		// Reads an input number
    		for (int i = 0; i < numbers.length; i++) {
    			String numString = JOptionPane.showInputDialog(null,
    			"Enter a number: ",
    			"Assignment 5.1 Input", JOptionPane.QUESTION_MESSAGE);
    			
    		// Convert string into integer
    		numbers[i] = Integer.parseInt(numString);		
    		}
    		// Determines primes
    		if (num <  2) return false;
    		if (num == 2) return true;
    		int n = (int) Math.sqrt(num);
    		for (int i = 3; i < n; i += 2) {
    			if (num&#37;i == 0) return false;
    		}
    		return true;
    	}
    	public void main(String[] args) {
    		//Declares "primes" as my variable
    		int[] primes = new int[1000];	
    		primes[0] = 2;				
    		int count = 1;
    		int num = 3;
    		// Finds the first 1000 primes
    		while (count<1000) {
    			if (isPrime(num)) {
    				primes[count] = num; 
    				count++;
    			}
    			num = num + 2;
    		}
    		// Prepares the output
    		String output = "The first 1000 primes are: ";
    		for (int i=0; i<primes.length; i++)
    		
    		output += primes[i] + "\t";
    		 		
    			// Display the result
    		JOptionPane.showMessageDialog(null, output, 
    			"Assignment 5.1 Output", JOptionPane.INFORMATION_MESSAGE);
    		
    		System.exit(0);
    	}
    }
    It shows as building properly, but before it runs I get this error:
    --------------------Configuration: <Default>--------------------
    java.lang.NoSuchMethodError: main
    Exception in thread "main"
    Process completed.
    ???
    Last edited by Drazzminius; Dec 7th, 2009 at 07:54 AM. Reason: Updated coding

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Location
    Okinawa, Japan
    Posts
    6

    Re: Prime Number Help

    Here is the latest version. I just can't seem to get the columns to work right.

    Code:
    import javax.swing.JOptionPane;
    
    public class PrimeNumberTest {	
    		
    		// Check if an integer is a prime
    	public static boolean isPrime(int num) {
    		if (num <  2) return false;
    		if (num == 2) return true;
    		int n = (int) Math.sqrt(num);
    		for (int i=3; i<n; i+=2) {
    			if (num&#37;i == 0) return false;
    		}
    		return true;
    	}
    	// Main Method
    	public static void main(String[] args) {
    		int[] primes = new int[1000];	// Define the array of the first 1000 primes
    		primes[0] = 2;					// The smallest prime is 2;
    		int count = 1;
    		int num = 3;
    		// Find the first 1000 primes
    		while (count<1000) {
    			if (isPrime(num)) {
    				primes[count] = num; 
    				count++;
    			}
    			num = num + 2;
    		}
    		// Opens the Welcome Message
    		JOptionPane.showMessageDialog(null, "Click OK to see all prime numbers under 10000", 
    		"Assignment 5.1", JOptionPane.INFORMATION_MESSAGE);
    		
    		// Prepares the output
    		String output = "The first 1000 primes are: ";
    		for (int i=0; i<primes.length; i++)	{
    		//	if (i%10 == 0), primes[i] ("");
    			output += primes[i] + "\n";}
    		 		
    			// Display the result
    		JOptionPane.showMessageDialog(null, output, 
    			"Assignment 5.1 Output", JOptionPane.INFORMATION_MESSAGE);
    		
    		System.exit(0);
    	}
    }

  4. #4
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Prime Number Help

    Have a look in the math forum! There might be a java example for prime numbers in there.
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

Posting Permissions

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



Click Here to Expand Forum to Full Width