package exercises; import java.time.Duration; import java.time.LocalTime; /** * * @author Lefteris Moussiades */ public class PrimeNumber { private int value; PrimeNumber(int val) { if (!isPrime(val)) throw new RuntimeException(); value=val; } 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) { 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++<100000); 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(); } }