Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package il.ac.tau.cs.franji;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.stream.StreamSupport;
- /**
- * Printing N first primary numbers - several ways to do it in Java
- * https://pastebin.com/tQdxerxD
- */
- public class Primes {
- public static void printNPrimesArray(int N) {
- int[] primes = new int[N];
- int nextIndex = 0;
- int k = 1; //starts with 2 as there is a += at loop start
- while (nextIndex < N) {
- k += 1;
- // check if k prime
- boolean isPrime = true;
- for (int i = 0; i < nextIndex; i++) {
- int p = primes[i];
- if ((k % p) == 0) {
- isPrime = false;
- break;
- }
- }
- if (isPrime) {
- primes[nextIndex++] = k;
- }
- }
- // now print the primes
- for (int p : primes) {
- System.out.println(p);
- }
- }
- public static void printNPrimesArrayList(int N) {
- ArrayList<Integer> primes = new ArrayList<>();
- int k = 1; //starts with 2 as there is a += at loop start
- while (primes.size() < N) {
- k += 1;
- // check if k prime
- boolean isPrime = true;
- for (int p : primes) {
- if ((k % p) == 0) {
- isPrime = false;
- break;
- }
- }
- if (isPrime) {
- primes.add(k);
- }
- }
- // now print the primes
- for (int p : primes) {
- System.out.println(p);
- }
- }
- public static void printNPrimesStreams(int N) {
- ArrayList<Integer> primes = new ArrayList<>();
- int k = 1; //starts with 2 as there is a += at loop start
- while (primes.size() < N) {
- k += 1;
- // check if k prime
- final int kk = k;
- if (primes.stream().allMatch(p -> (kk % p) != 0)) {
- primes.add(k);
- }
- }
- // now print the primes
- for (int p : primes) {
- System.out.println(p);
- }
- }
- static class PrimesIteratable implements Iterable<Integer> {
- ArrayList<Integer> primes = new ArrayList<>();
- class PrimesIterator implements Iterator<Integer> {
- int k = 1;
- @Override
- public boolean hasNext() {
- return true; // primes is infinite
- }
- @Override
- public Integer next() {
- for (; ; ) {
- this.k++;
- boolean isPrime = true;
- boolean addToPrimes = true;
- for (int p : primes) {
- if (p >= k) {
- // the primes array may have more values since it was built by another iterator
- addToPrimes = false;
- isPrime = p == k;
- break;
- }
- if ((k % p) == 0) {
- isPrime = false;
- break;
- }
- }
- if (isPrime) {
- if (addToPrimes) {
- // note - adding to parent class array - shared by all iterators
- primes.add(k);
- }
- return k;
- }
- }
- }
- }
- @Override
- public Iterator<Integer> iterator() {
- return new PrimesIterator();
- }
- }
- public static void main(String[] args) {
- System.out.println("--- printNPrimesArray(20) ---");
- printNPrimesArray(20);
- System.out.println("--- printNPrimesArrayList(20) ---");
- printNPrimesArrayList(20);
- System.out.println("--- printNPrimesStreams(20) ---");
- printNPrimesStreams(20);
- System.out.println("--- PrimesIteratable -take 1 (30) ---");
- // primesIter is an infinite list of primes you can iterate over.
- PrimesIteratable primesIter = new PrimesIteratable();
- int i = 0;
- for (int p : primesIter) {
- System.out.println(p);
- if (i++ >= 30) {
- break;
- }
- }
- System.out.println("--- PrimesIteratable -take 2 (20) ---");
- // Using the same iterable again - primes array inside it is already initialized
- i = 0;
- for (int p : primesIter) {
- System.out.println(p);
- if (i++ >= 20) {
- break;
- }
- }
- System.out.println("--- PrimesIteratable -convert to stream limit(20) ---");
- StreamSupport.stream(primesIter.spliterator(), false).limit(20).forEach(p -> System.out.println(p));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement