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"));
}
}
}
cHVibGljIGNsYXNzIFExNzU4NzUzMgp7CiAgICBzdGF0aWMgaW50W10gR2VuZXJhdGVEaWdpdFByb2R1Y3RzKCBpbnQgbWF4ICkKICAgIHsKICAgICAgICBpbnQgc3dlZXAgPSAxOwogICAgICAgIHZhciByZXN1bHRzID0gbmV3IGludFttYXgrMV07CiAgICAgICAgZm9yKCBpbnQgaSA9IDE7IGkgPD0gOTsgKytpICkgcmVzdWx0c1tpXSA9IGk7CiAgICAgICAgLy8gbG9vcCBpbnZhcmlhbnQ6IGFsbCB2YWx1ZXMgdXAgdG8gc3dlZXAgKiAxMCBhcmUgZmlsbGVkIGluCiAgICAgICAgd2hpbGUgKHRydWUpIHsKICAgICAgICAgICAgaW50IHByaW9yID0gcmVzdWx0c1tzd2VlcF07CiAgICAgICAgICAgIGlmIChwcmlvciA+IDApIHsKICAgICAgICAgICAgICAgIGZvciggaW50IGogPSAxOyBqIDw9IDk7ICsraiApIHsKICAgICAgICAgICAgICAgICAgICBpbnQgayA9IHN3ZWVwICogMTAgKyBqOyAvLyA8LS0gdGhlIGtleSwgZ2VuZXJhdGluZyBudW1iZXIgZnJvbSBkaWdpdHMgaXMgbXVjaCBmYXN0ZXIgdGhhbiBkZWNvbXBvc2luZyBudW1iZXIgaW50byBkaWdpdHMKICAgICAgICAgICAgICAgICAgICBpZiAoayA+IG1heCkgcmV0dXJuIHJlc3VsdHM7CiAgICAgICAgICAgICAgICAgICAgcmVzdWx0c1trXSA9IHByaW9yICogajsKICAgICAgICAgICAgICAgICAgICAvLyBsb29wIGludmFyaWFudDogYWxsIHZhbHVlcyB1cCB0byBrIGFyZSBmaWxsZWQgaW4KICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICArK3N3ZWVwOwogICAgICAgIH0KICAgIH0KICAgIAogICAgcHVibGljIHN0YXRpYyB2b2lkIE1haW4oKQogICAgewogICAgICAgIGludFtdIHByb2R1Y3RzID0gR2VuZXJhdGVEaWdpdFByb2R1Y3RzKDk5KTsKICAgICAgICAKICAgICAgICBmb3IoIGludCBuID0gMjE7IG4gPD0gOTk7ICsrbiApIHsKICAgICAgICAgICAgU3lzdGVtLkNvbnNvbGUuV3JpdGVMaW5lKG4uVG9TdHJpbmcoIkQyIikgKyAiID0+ICIgKyBwcm9kdWN0c1tuXS5Ub1N0cmluZygiRDMiKSk7CiAgICAgICAgfQogICAgfQp9