Question
Assignment: Research and implement the Sieve of Eratosthenes (also called prime sieve) algorithm. Researching and implementing algorithms is something I did frequently while consulting and
Assignment:
Research and implement the Sieve of Eratosthenes (also called prime sieve) algorithm. Researching and implementing algorithms is something I did frequently while consulting and any programmer must be able to do this.
Psuedo Code provided:
Declare an array num, Input limi,t currentPrime = 2;
do{
for(currentPrime + 1 to limit){ // set all evenly divided by CP to zero
if(num[i]%currentPrime == 0)// see if non prime
num[i] = 0;// not prime set to zero } // next non-zero is the next prime, find it.
For(currentPrime + 1 to limit)
If(num[i]!=0){
currentPrime = num[i];
break; }
While(!atEnd);
The code I have:
public class PrimeSieve { public static void main(String[] args) { int limit = 15; //initialize max number, find primes <= to limit int currentPrime = 2; //initialize array int[] num = new int[limit]; do { for (i = 2; i< limit; i++) { if(num[i]%currentPrime == 0) //sets indices divided by cP to zero num[i] = 0; // non prime set to zero } for (int i = 2; i< limit; i++) { if(num[i]!=0) { currentPrime = num[i]; break; } System.out.println(currentPrime); } } while (currentPrime != limit); } }
The error i encounter is it compiles but when i run it gives 2 repeatedly and i have to terminate the program manually. Not sure where my error is exactly and what i should do to correct.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started