Results 1 to 5 of 5

Thread: PrimeNumber

  1. #1
    zit1343 is offline Member
    Join Date
    Jan 2011
    Posts
    17
    Rep Power
    0

    Default PrimeNumber

    Hey
    this is not my homework
    I am studying my first test for final

    here is the question
    please help me

    Consider the following unfinished class:
    Java Code:
    public class MakePrimes {
             int v[];
             public MakePrimes () {
                      v = new int[100];
             }
             boolean isPrime (int n) {
                        ???
             }
             public void fillV() {
                      ???
             }
    thank you

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Ask a more specific question, and show some more work. What are you trying to do? Sieve of Eratosthenes? Just a regular approach? Try it and we will help you and possibly offer optimization ideas. For instance, did you know that when searching for a prime you will know whether it is prime by the time you reach the square root of n? Also, after testing 2, you can skip all even numbers from then on(2, 3, 5, 7, 9, etc.)

  3. #3
    oomrichie is offline Member
    Join Date
    Feb 2011
    Posts
    31
    Rep Power
    0

    Default

    I found this class using a quick Google search

    Java Code:
    /*
        This class writes out Prime numbers. A number is prime if it is 
        divisible by 1 and the number itself and no other number.
    */
    
    public class PrimeNumber
    {
      // This method tests whether a given number is prime or not.
      public static boolean isPrime ( int num )
      {
        boolean prime = true;
        int limit = (int) Math.sqrt ( num );  
    
        for ( int i = 2; i <= limit; i++ )
        {
          if ( num % i == 0 )
          {
            prime = false;
    	break;
          }
        }
    
        return prime;
      }
    
      public static void main ( String[] args )
      {
        // This loop writes out all the prime numbers less than 1000.
        for ( int i = 2; i <= 1000; i++ )
        {
          if ( isPrime ( i ) )
    	System.out.println ( i );
        }
      }
    }

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I understand the intentions here are good oomrichie, however; please don't spoonfeed. If he was pushed towards the right answer he would get a lot more help than just being given the answer. I know you found this on google and so could he have. Still, it doesn't help him learn much.

  5. #5
    oomrichie is offline Member
    Join Date
    Feb 2011
    Posts
    31
    Rep Power
    0

Similar Threads

  1. Java primenumber application
    By Crossfires in forum New To Java
    Replies: 9
    Last Post: 10-12-2009, 06:06 PM

Posting Permissions

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