package exercises; import java.time.Duration; import java.time.LocalTime; /** * * @author Lefteris Moussiades */ public class PrimeNumber { private int value; public PrimeNumber(int val) { if (!isPrime(val)) throw new RuntimeException(); value=val; } public static boolean isPrime(int val) { if (val<=1) return false; for (int i=2; i<=val/2; i++) if (val%i==0) return false; return true; } public int getValue() { return value; } public void setValue(int value) { if (!isPrime(value)) throw new RuntimeException(); this.value = value; } static int getNext(int val) { do { if (isPrime(++val)) return val; } while (true); } PrimeNumber getNext() { return new PrimeNumber(getNext(value)); } public String toString() { return String.valueOf(value); } static void prt100First() { LocalTime tArx=LocalTime.now(); PrimeNumber prime=new PrimeNumber(getNext(0)); int j=0; do { System.out.println(j+" "+prime+" "); prime=prime.getNext(); } while (j++<100); LocalTime tTel=LocalTime.now(); Duration d=Duration.between(tArx, tTel); System.out.println(d.getSeconds()+":"+d.getNano()); } public static void main(String[] args) { /* System.out.println(isPrime(1)); PrimeNumber p1=new PrimeNumber(7); int i=0; while (i<100) { i=PrimeNumber.getNext(i); System.out.print(i+" "); } System.out.println("---------------------------------"); for (i=0; i<100; i++) if (isPrime(i)) System.out.print(i+", "); System.out.println(); System.out.println("2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97"); */ prt100First(); //for (int i=1; i<10; i++) // if (PrimeNumber.isPrime(i)) System.out.println(i+" "); } }