473,327 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

A little help with prime number generator, please

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
  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 3556
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
  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
Thank's! That solved my problem!

- maks
Jan 3 '07 #3
Ganon11
3,652 Expert 2GB
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 Expert 2GB
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 Expert 2GB
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

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Greg Brunet | last post by:
In doing some testing of different but simple algorithms for getting a list of prime numbers, I ended up getting some results that seem a bit contradictory. Given the following test program...
36
by: Profetas | last post by:
Hi, I want to generate a random 8 bit number using rand(0 is that possible? to expecifu the base and the lenght? thanks
2
by: wudoug119 | last post by:
This is my code and it will take any number that I input and say it is a prime number. Please help me... int Prime(int prime) //declares isPrime as a function using integers { ...
4
by: SweetLeftFoot | last post by:
Hello, i have designed some code that works out the first 250 prime numbers and prints them to the screen. However i need to implement 2 functions, one of which returns a 1 if the number is a prime...
10
by: Joel Mayes | last post by:
Hi All; I'm teaching myself C, and have written a prime number generator. It is a pretty inefficient implementation of the Sieve of Eratosthenes to calculate primes up to 1,000,000. If anyone...
4
bartonc
by: bartonc | last post by:
Description: This is a fast prime number list generator using sieve algorithm. This function return a list of prime numbers which <= argument. def primes(n): if n==2: return elif n<2:...
2
by: QHorizon | last post by:
Hello, I'm new to Python (I've learned everything up to iterators so far) and fairly new to Programming. This would be my first real program: #Coordinate Geometry (The whole program is not...
4
by: cnixuser | last post by:
Hello, I am attempting to create a prime number detector that will identify all of the prime numbers within a specified range of numbers. The problem is, for some reason my program is not detecting...
6
by: sigkill9 | last post by:
I'm doing some reading in a python book and am doing one of the chapter exercises but I cant figure out how to get it to work and was hoping some of you python guru's could help out? Heres...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.