438,034 Members | 820 Online Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

# A little help with prime number generator, please

 P: 5 Hi! I need some help in modifying this prime number generator code. How do I modify this code so that it assigns prime numbers to an array and returns it? I have tried to get it work but it won't, so please help me. Expand|Select|Wrap|Line Numbers class Prime2 { int[] array = new int;   public int[] Primes() {      int i = 0;   for (i = 0; i < array.length; i++)    {     int x, y  = 0;       for( x = 2; x < 100; x++ )     {       if( x % 2 != 0 || x == 2 )       {         for( y = 2; y <= x / 2; y++ )         {           if( x % y == 0 )           {             break;           }            }           if( y > x / 2 )         {          array[i] = x;          }     } } } return array; }     public static void main (String[] args) {       Prime2 numbers = new Prime2();     System.out.println(numbers.Primes());   }   }     - maks Dec 30 '06 #1
5 Replies

 P: 38 Hi! I need some help in modifying this prime number generator code. How do I modify this code so that it assigns prime numbers to an array and returns it? I have tried to get it work but it won't, so please help me. Expand|Select|Wrap|Line Numbers class Prime2 { int[] array = new int;   public int[] Primes() {      int i = 0;   for (i = 0; i < array.length; i++)    {     int x, y  = 0;       for( x = 2; x < 100; x++ )     {       if( x % 2 != 0 || x == 2 )       {         for( y = 2; y <= x / 2; y++ )         {           if( x % y == 0 )           {             break;           }            }           if( y > x / 2 )         {          array[i] = x;          }     } } } return array; }     public static void main (String[] args) {       Prime2 numbers = new Prime2();     System.out.println(numbers.Primes());   }   }     - maks Sorry about the slow reply but had to find out that hotmail wasnt accepted by thescripts.com, anyways as for your problem. Your method of making the array looks fine. However your print method is wrong, this is a mistake made by beginners such as me alot. What you will get is, if i remember it correct, the unice hash code of the object in this case the array you are trying to print. What you need to do is override the toString method in the class Object. The toString method should look something like this: Expand|Select|Wrap|Line Numbers public String toString() {   String s = ""; // just so your compiler will not give u problems such as the local                      // variable s may not have been initialized.    for(int i = 0; i

 P: 5 Thank's! That solved my problem! - maks Jan 3 '07 #3

 Expert 2.5K+ P: 3,652 Expand|Select|Wrap|Line Numbers public int[] Primes() {      int i = 0;     for (i = 0; i < array.length; i++) {         int x, y  = 0;           for( x = 2; x < 100; x++ ) {             if( x % 2 != 0 || x == 2 ) {                 for( y = 2; y <= x / 2; y++ ) {                     if( x % y == 0 ) {                         break;                     }                    }                      if( y > x / 2 ) {                     array[i] = x;                 }             }         }     }     return array; } So far, it looks like you are going to generate the same prime number for each element in array. Your first loop is looping i from 0 to 24 to assign elements to array - which is good. But the inside of the loop is constant - that is, it doesn't depend on i (until you assign the value to the array). You should modify the inside of the loop so that it is dependent on i. Jan 4 '07 #4

 Expert 2.5K+ P: 3,652 Also, the inside of your loop will loop through every possible prime number from 2 to 100. If that number is prime, it is assigned to a location in array. But you continue executing, even after a prime number has been found. Shouldn't you break as soon as you find a prime number, so that you aren't constantly re-assigning values to the same location? You may want to define x outside of your initial loop. You will be generating values for each element in the array, so that outside loop is fine. Inside your first loop, instead of using a for loop, why not use a while loop to continue checking if x is prime - if x is prime, break out of the while loop, else, increment x. Once outside of the while loop, assign the ith value of array to x and end the loop. You may want to define a function Expand|Select|Wrap|Line Numbers private boolean isPrime(int x); to clean up your code a bit. Jan 4 '07 #5

 Expert 2.5K+ P: 3,652 Sorry for using three posts, but there's some error that kept me from posting the whole response as one post. Jan 4 '07 #6 