The Artima Developer Community
Sponsored Link

Java Answers Forum
Prime Integers

3 replies on 1 page. Most recent reply: Nov 13, 2002 7:10 AM by Dante T. Salvador

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Marie Harris

Posts: 6
Nickname: match
Registered: Nov, 2002

Prime Integers Posted: Nov 11, 2002 8:19 PM
Reply to this message Reply
Advertisement
Listed below are question and attempted solution. However there are some errors. Can someone please assist. Thanks

(The Sieve of Eratosthenes) A prime integer is any integer that is evenly divisible only by itself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows:

Create an array with all elements initialized to 1 (true). Array elements with prime subscripts will remain 1. All other array elements will eventually be set to zero.
Starting with array subscript 2 (subscript 1 must be prime), every time an array element is found whose value is 1, loop through the remainder of the array and set to zero every element whose subscript is a multiple of the subscript for the element with value 1. For array subscript 2, all elements beyond 2 in the array that are multiples of 2 will be set to zero (subscripts 4, 6, 8, 10, etc.); for array subscript 3, all elements beyond 3 in the array that are multiples of 3 will be set to zero (subscripts 6, 9, 12, 15, etc.); and so on.

// Solution
// PrimeNum.java

// Program searches for and prints the prime numbers between 1 and 999 inclusive

//****************************************************************** ***
import javax.swing.*;

public class PrimeNum {

// main method begins execution of Java application
public static void main( String args[] ) {

// Declare an array: arrayc with 1000 elements
boolean arrayc[]; // reference to int array
arrayc = new boolean[1000]; // allocate array


// Initialization & Declaration of variables
for ( int index = 0; index < arrayc.length; index++ )
arrayc[index] = true;
String output = "";


// Call method to search for multiples of index between 2 and 999
primeSearch(arrayc);

} // method main

// ************************************************************
// Method: primeSearch to search for multiples of the current index

public static void primeSearch( boolean arrayNum[] ) {

int count = 0;

// Initiate loop to find multiples of the index between 2 and 999
for ( int i = true; i < arrayNum.length; i++ )
if ( arrayNum = true & i != true)
for ( int counter = i; counter < arrayNum.length; counter++ )
if(counter%i = false & counter != i)
arrayNum[counter] = false;

// Print prime numbers:
String output = "Prime Numbers\n";

for ( int k = 2; k <arrayNum.length; k++ ){
if (arrayNum[k] = true)
output += k +"\n";
++count;

} // close third for

JTextArea outputArea = new JTextArea();
outputArea.setText( output );

JOptionPane.showMessageDialog( null, outputArea,
"Prime Numbers between 1 and 999",
JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 );

} // end method primeSearch

} // end class PrimeNum


Dante T. Salvador

Posts: 19
Nickname: anteng
Registered: Nov, 2002

Re: Prime Integers Posted: Nov 12, 2002 9:07 AM
Reply to this message Reply


for (int i = 2; i < 1000; i++) { // begin for
prime = false;
for (int j = 2; j <= i /2 j ++ ) { // begin for
if (!(i%j)) prime = true;

if (prime) System.out.println( i + " is prime");
} // end for
} // end for


try this code. i not sure if this things run. go back here if you have a problem.

Marie Harris

Posts: 6
Nickname: match
Registered: Nov, 2002

Re: Prime Integers Posted: Nov 12, 2002 7:40 PM
Reply to this message Reply
Thanks for the solution, however the question ask for the use of a boolean array.

Dante T. Salvador

Posts: 19
Nickname: anteng
Registered: Nov, 2002

Re: Prime Integers Posted: Nov 13, 2002 7:10 AM
Reply to this message Reply

public class primeBoolean { // begin
int start;
int end;
boolean[] bolArray;
// constructor
primeBoolean(){
start = 0;
end = 0;
}
// constructor: initialize the start and
primeBoolean(int st, int nd ){
start = st;
end = nd;
int i = (nd - st) + 1;
bolArray = new boolean[i];
}
// return the index of array to be used
public int getIndex() {
int i = (this.end - this.start) + 1;
return i;
}
// initialize array element to defualt value of true
public void initArray(){
for (int x = 0;x < bolArray.length ;x++)
bolArray[x] = false;
}
// intialize array element to its argument
public void initArray(boolean bln){
for (int x = 0;x < bolArray.length ;x++)
bolArray[x] = bln;
}
// print the array without index
public void printArray(){
for (int x = 0;x < bolArray.length ;x++)
System.out.println(bolArray[x] + " ");
}
// print the array with index
public void printArrayWithIndex(){
int z = this.start;
for (int x = 0;x < bolArray.length ;x++) {
System.out.println("Index " + z + " : " + bolArray[x]);
z++;
}
}
// print the argument array without index
public void printArray(boolean[] arr){
for (int x = 0;x < arr.length ;x++)
System.out.println(arr[x] + " ");
}
// print the argument array with index
public void printArrayWithIndex(boolean[] arr){
int z = this.start;
for (int x = 0;x < arr.length ;x++){
System.out.println("Index " + z + " : " + arr[x]);
z++;
}
}
// determine if the argument number is prime and
// return true, otherwise false
public boolean isPrime(int number) {
int limit = (int)Math.sqrt(number);
int denum = 2;
boolean prime = true;
while (denum <= limit) { // begin while
if ((number % denum) == 0)
prime = false;
denum++;
} // end while
return prime;
} // end
// return the prime boolean array
public boolean[] determinePrime(){ // begin
boolean prime;
int count = start;
int index = this.getIndex();
boolean[] tmp = new boolean[index];
int indexCounter = 0;
while (count <= end) { // begin
prime = isPrime(count);
if(prime == true)
tmp[indexCounter] = true;
else
tmp[indexCounter] = false;
indexCounter++;
count++;
} // end while
bolArray = tmp;
return tmp;
} // end
public static void main(String[] args) {
primeBoolean prime = new primeBoolean(100,200);
int index = prime.getIndex();
boolean[] anotherArray;
anotherArray = new boolean[index];
prime.initArray(true);
anotherArray = prime.determinePrime();
prime.printArrayWithIndex();
}
} // end class

Flat View: This topic has 3 replies on 1 page
Topic: illegal start of expresion Previous Topic   Next Topic Topic: Java RMI help needed please

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use