473,840 Members | 1,594 Online

# A little help with prime number generator, please

5 New Member
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?

Expand|Select|Wrap|Line Numbers
1. class Prime2
2. {
3. int[] array = new int[25];
4.
5. public int[] Primes() {
6.
7.   int i = 0;
8.   for (i = 0; i < array.length; i++)
9.   {
10.     int x, y  = 0;
11.     for( x = 2; x < 100; x++ )
12.     {
13.       if( x % 2 != 0 || x == 2 )
14.       {
15.         for( y = 2; y <= x / 2; y++ )
16.         {
17.           if( x % y == 0 )
18.           {
19.             break;
20.           }
21.         }
22.
23.         if( y > x / 2 )
24.         {
25.          array[i] = x;
26.          }
27.     }
28. }
29. }
30. return array;
31. }
32. public static void main (String[] args) {
33.
34.     Prime2 numbers = new Prime2();
35.     System.out.println(numbers.Primes());
36.
37. }
38.
39. }
40.
41.

- maks
Dec 30 '06 #1
5 3586
BSCode266
38 New Member
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?

Expand|Select|Wrap|Line Numbers
1. class Prime2
2. {
3. int[] array = new int[25];
4.
5. public int[] Primes() {
6.
7.   int i = 0;
8.   for (i = 0; i < array.length; i++)
9.   {
10.     int x, y  = 0;
11.     for( x = 2; x < 100; x++ )
12.     {
13.       if( x % 2 != 0 || x == 2 )
14.       {
15.         for( y = 2; y <= x / 2; y++ )
16.         {
17.           if( x % y == 0 )
18.           {
19.             break;
20.           }
21.         }
22.
23.         if( y > x / 2 )
24.         {
25.          array[i] = x;
26.          }
27.     }
28. }
29. }
30. return array;
31. }
32. public static void main (String[] args) {
33.
34.     Prime2 numbers = new Prime2();
35.     System.out.println(numbers.Primes());
36.
37. }
38.
39. }
40.
41.

- 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
1. public String toString()
2. {
3.   String s = ""; // just so your compiler will not give u problems such as the local
4.                      // variable s may not have been initialized.
5.    for(int i = 0; i<array.length; i++)
6.    {
7.     s += array[i];
8.    }
9.    return s;
10. }
11.
Try to look in your textbook if you have one, toString methods are usually explained in those, if not try looking for something on the internet.
Once you implement this toString method you will see that you need some tweaking in the mathematical part of your code. You should also take note that methodnames usually start with lowercase letters.

~BSCode266
Jan 2 '07 #2
maks
5 New Member
Thank's! That solved my problem!

- maks
Jan 3 '07 #3
Ganon11
3,652 Recognized Expert Specialist
Expand|Select|Wrap|Line Numbers
1. public int[] Primes() {
2.     int i = 0;
3.     for (i = 0; i < array.length; i++) {
4.         int x, y  = 0;
5.         for( x = 2; x < 100; x++ ) {
6.             if( x % 2 != 0 || x == 2 ) {
7.                 for( y = 2; y <= x / 2; y++ ) {
8.                     if( x % y == 0 ) {
9.                         break;
10.                     }
11.                 }
12.
13.                 if( y > x / 2 ) {
14.                     array[i] = x;
15.                 }
16.             }
17.         }
18.     }
19.     return array;
20. }
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
Ganon11
3,652 Recognized Expert Specialist
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
1. private boolean isPrime(int x);
to clean up your code a bit.
Jan 4 '07 #5
Ganon11
3,652 Recognized Expert Specialist
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