Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)
Paste
Pasted by zeeshan ( 16 years ago )
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package support;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author admin
*/
public class SieveOfErastothenes {
double upperLimit ;
List<Double> primeNumbers ;
public SieveOfErastothenes(double upperLimit){
this.upperLimit = upperLimit;
primeNumbers = new ArrayList<Double>() {};
for(double i = 2 ; i <= upperLimit ; i++)
primeNumbers.add(i);
generatePrimes();
}
private void generatePrimes(){
int currentPrimeIndex = 0;
double currentPrime = 2;
while(currentPrime <= Math.sqrt(upperLimit)){
//System.out.println("Numbers removed now are: ");
double multiplier = 2;
while (currentPrime * multiplier <= upperLimit){
//System.out.print(currentPrime * multiplier+" ");
primeNumbers.remove((currentPrime * multiplier));
multiplier++;
}
//System.out.println();
currentPrimeIndex++;
currentPrime = primeNumbers.get(currentPrimeIndex);
}
}
public List getPrimes(){
return primeNumbers;
}
public void displayPrimes(){
for(double i : primeNumbers)
System.out.println(i);
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package projecteuler;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import support.SieveOfErastothenes;
/**
*
* @author admin
*/
public class Problem10 implements NormalProblem{
private double upperLimit;
private double sumOfPrimes;
public void getInput() {
try {
System.out.println("Enter the upper limit");
upperLimit = Integer.parseInt(br.readLine());
} catch (IOException ex) {
Logger.getLogger(Problem10.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void execute() {
double sum = 0;
SieveOfErastothenes soe = new SieveOfErastothenes(upperLimit);
ArrayList<Double> primeNumbers = (ArrayList<Double>)soe.getPrimes();
//soe.displayPrimes();
for(double i : primeNumbers ){
sum = sum + i;
}
sumOfPrimes = sum;
}
public void printOutput() {
System.out.println(sumOfPrimes);
}
}
Revise this Paste