public class Q17587532 { static int[] GenerateDigitProducts( int max ) { int sweep = 1; var results = new int[max+1]; for( int i = 1; i <= 9; ++i ) results[i] = i; // loop invariant: all values up to sweep * 10 are filled in while (true) { int prior = results[sweep]; if (prior > 0) { for( int j = 1; j <= 9; ++j ) { int k = sweep * 10 + j; // <-- the key, generating number from digits is much faster than decomposing number into digits if (k > max) return results; results[k] = prior * j; // loop invariant: all values up to k are filled in } } ++sweep; } } public static void Main() { int[] products = GenerateDigitProducts(99); for( int n = 21; n <= 99; ++n ) { System.Console.WriteLine(n.ToString("D2") + " => " + products[n].ToString("D3")); } } }