import java.io.*; import java.util.*; public class WeirdNumbers { public static void main(String[] args) { final int max = 1000000; // find weird numbers up to and including this final String fname = "weird1M.txt"; // and store them in this file TreeSet factorSet=new TreeSet(); Set semiperfSet = new HashSet(); int factorSum=1,abun,factor; try { PrintStream outFile=new PrintStream(new FileOutputStream(fname)); long rt=System.currentTimeMillis(); loop:for(int theNumber=2;theNumber<=max;theNumber+=2,factorSum=1) { factorSet.clear(); for(int ii=2;ii smallFactorSet = (TreeSet)factorSet.subSet(1,abun); Integer smallFactorArr[] = smallFactorSet.toArray(new Integer[smallFactorSet.size()]); // check if any sum of factors < abun can be added up to abun if(backTrack(smallFactorArr,0,abun)) { semiperfSet.add(theNumber); // store this semiperfect number for later checks continue; // and continue, since no semiperfect number can be weird. } outFile.println(theNumber); // found a weird number, put it into file } System.out.println((System.currentTimeMillis()-rt)+"ms"); outFile.close(); } catch(Exception e) { e.printStackTrace(); } } static boolean backTrack(Integer a[],int k,int n) { if(n>=a[k]) { if(0==(n-=a[k])) return true; if(k+1