nᵗʰ ugly number
A simple fast solution in Java. Uses approach described by Anon.. Here TreeSet is just a container capable of returning smallest element in it. (No duplicates stored.) int n = 20; SortedSet<Long> next = new TreeSet<Long>(); next.add((long) 1); long cur = 0; for (int i = 0; i < n; ++i) { cur = next.first(); … Read more