• 05-01-2011, 07:31 PM
zit1343
Hey
this is not my homework
I am studying my first test for final

here is the question

Consider the following unfinished class:
Code:

```public class MakePrimes {         int v[];         public MakePrimes () {                   v = new int[100];         }         boolean isPrime (int n) {                     ???         }         public void fillV() {                   ???         }```
thank you
• 05-01-2011, 07:39 PM
sunde887
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.)
• 05-01-2011, 07:44 PM
oomrichie
I found this class using a quick Google search

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 );     }   } }```
• 05-01-2011, 07:47 PM
sunde887
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.
• 05-01-2011, 07:52 PM
oomrichie