public class ThreesAndFives { private static final int LIMIT = 10_000_000; // = 10^7 public static void main(String[] args) { int threes = 3; int fives = 5; while (threes <= LIMIT || fives < LIMIT) { final int smaller = Math.min(threes, fives); System.out.println(smaller); if (threes == smaller) { threes += 3; } if (fives == smaller) { fives += 5; } } } }