aboutsummaryrefslogtreecommitdiffstats
path: root/csci1913/Java
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--csci1913/Java/lab5_strap012.java33
1 files changed, 29 insertions, 4 deletions
diff --git a/csci1913/Java/lab5_strap012.java b/csci1913/Java/lab5_strap012.java
index f7f9467..45a101b 100644
--- a/csci1913/Java/lab5_strap012.java
+++ b/csci1913/Java/lab5_strap012.java
@@ -10,10 +10,35 @@
//
// Put your code for the class SIEVE here!!!
//
-class sieve {
-
- public sieve(){
- for
+class Sieve {
+ private boolean[] numbers;
+ private int realMax;
+ public Sieve(int max){
+ if (max<2)
+ throw new IllegalArgumentException();
+ numbers = new boolean[max];
+ realMax = (max - 1);
+ for (int i=2; i<max; i+=1) { numbers[i]=true; }
+ }
+ public void findPrimes() {
+ for (int i=2; i<=realMax; i+=1) {
+ //Only look for composite numbers if the number is prime
+ if (numbers[i]) {
+ for (int j=(i*2); j<=realMax; j+=i) {
+ numbers[j]=false;
+ }
+ }
+ // System.out.println(i);
+ }
+ }
+ public String toString(){
+ String str = "";
+ for (int n = 0; n <= realMax; n += 1) {
+ if (numbers[n]) {
+ str += n; str += " ";
+ }
+ }
+ return str;
}
}
// TOSS THE KNEES. Run SIEVE on some examples.