diff options
author | Julian T <julian@jtle.dk> | 2020-09-08 13:43:21 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-09-08 13:43:21 +0200 |
commit | c009693a07276f8d66cd68637431f0a3f0e9ef15 (patch) | |
tree | fc9bc85620813e7a179b043203d25879932267f6 /sem5/oop/m2/primes/src/PrimeGen.java | |
parent | ba5f68924664b081aab1abb62c68e2fb70bdff0b (diff) |
Added more java assignments
Diffstat (limited to 'sem5/oop/m2/primes/src/PrimeGen.java')
-rw-r--r-- | sem5/oop/m2/primes/src/PrimeGen.java | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/sem5/oop/m2/primes/src/PrimeGen.java b/sem5/oop/m2/primes/src/PrimeGen.java new file mode 100644 index 0000000..9c91645 --- /dev/null +++ b/sem5/oop/m2/primes/src/PrimeGen.java @@ -0,0 +1,40 @@ +import java.io.PrintStream; +import java.util.ArrayList; + +public class PrimeGen { + + private ArrayList<Integer> primes; + private int at; + + public PrimeGen() { + this.primes = new ArrayList<>(); + + this.at = 1; + } + + public int next(int limit) { + // Run until a prime + primeloop: + while (true) { + this.at++; + if (this.at >= limit) { + return -1; + } + + for (int prime : this.primes) { + if (this.at % prime == 0) { + continue primeloop; + } + } + break; + } + + this.primes.add(at); + return at; + } + + public int next() { + return next(-1); + } + +} |