import java.util.HashSet; import java.util.function.Predicate; /* java.util.function.Predicate is an interface, essentially same as: interface Predicate { boolean test(T x); } */ // Sets and Infinite Sets interface Set { boolean contains(D x); void add(D x); // destructive addition Set union(Set setB); Set intersection(Set setB); Set complement(); boolean subset(Set setB); Set clone(); } class Fset implements Set // finite sets using a java.util.HashSet { HashSet HS; public Fset() // constructor creates empty set { HS = new HashSet(); } public Fset(D[] A) // constructor creates set from array { HS = new HashSet(); for(D x:A) HS.add(x); } public boolean contains(D x) { return HS.contains(x); } public void add(D x) { HS.add(x); } public Set clone() { Fset Cloneset = new Fset(); for(D x:HS) Cloneset.add(x); // for loop on HashSet return Cloneset; } public Set union(Set B) { Set BC = B.clone(); // don't know what kind of set B is for(D x: HS) BC.add(x); return BC; } public Set intersection(Set B) { Set Intsect = new Fset(); for(D x:HS) if (B.contains(x)) Intsect.add(x); return Intsect; } public Set complement() { return new Pset( (x)->!HS.contains(x) ); } public boolean subset(Set B) { for(D x:HS) if (!B.contains(x)) return false; return true; } }//Fset class Pset implements Set // predicate sets of domain type D { protected Predicate p = (x)->false; // default predicate is empty set public Pset(Predicate q) { if (q!=null) p=q; } public boolean contains(D x) { return p.test(x); } public void add(D x) { Predicate p0 = p; // copy original p = (y) -> y.equals(x) || p0.test(x); // change predicate } public Set clone() { Predicate pc=p; return new Pset(pc); } public Set union(Set B) { return new Pset((x)-> this.contains(x) || B.contains(x)); } public Set intersection(Set B) { if (B instanceof Fset) return B.intersection(this); //??? why // indicates limitations of dynamic dispatch paradigm. return new Pset((x)-> this.contains(x) && B.contains(x)); } public Set complement() { return new Pset((x)->!this.contains(x)); } public boolean subset(Set B) { throw new RuntimeException("no general algorithm for Pset.subset"); } }//Pset public class sets2 { // testing if a number is prime public static boolean isprime(int n) { if (n==2) return true; // only even prime if (n<2 || n%2==0) return false; // rules out evens, <2 nums boolean answer = true; int limit = (int)(Math.sqrt(n)+1); for(int i=3;i Odds = new Pset( (n)->n%2==1 ); // set of all positive numbers: Set Positives = new Pset( (x)->x>0 ); // set of all positive even numbers Set Evens = Odds.complement().intersection(Positives); System.out.println(Evens.contains(8)); //true System.out.println(Evens.contains(9)); //false // finite set Integer[] P = {3,5,7,11,13}; var F = new Fset(P); F.add(2); // set of all primes: Set Primes = new Pset((x)->isprime(x)); System.out.println(Primes.contains(15)); //false System.out.println(Primes.contains(19)); //true // set of all finite subsets of primes Set> FSP = new Pset>((s)->s.subset(Primes)); System.out.println(FSP.contains(F)); // true // is every even number greater than 2 the sum of two primes? Set EG2 = new Pset((x)->x>2 && x%2==0); Set Sum2ps = new Pset( (x)-> sumof2primes(x) ); System.out.println(EG2.subset(Sum2ps)); //??? Goldbach's Conjecture }//main } // even .contains may not be definable, because .contains may never // return at all - Halting problem.