Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package scratch;
- import java.util.ArrayList;
- /** Rolling sieve of Eratothanes.
- */
- public class Sieve {
- public static void main(String[] args) {
- // max value to go to...
- int n = 100;
- // flag to say whether the current i si prime
- // assume it's prime to start with
- boolean iprime = true;
- ArrayList<Integer> primes = new ArrayList<Integer>();
- // base case
- primes.add(2);
- for (int i = 3; i <= n; i++) {
- for (int p : primes) {
- if (i % p == 0) {
- iprime = false;
- // no need to look at the rest of the prime list
- break;
- }
- }
- if (iprime) {
- primes.add(i);
- }
- // reset flag
- iprime = true;
- }
- // output the list
- for (int p : primes) {
- System.out.println(p);
- }
- }
- }
Add Comment
Please, Sign In to add comment