fork download
  1. public class Q17587532
  2. {
  3. static int[] GenerateDigitProducts( int max )
  4. {
  5. int sweep = 1;
  6. var results = new int[max+1];
  7. for( int i = 1; i <= 9; ++i ) results[i] = i;
  8. // loop invariant: all values up to sweep * 10 are filled in
  9. while (true) {
  10. int prior = results[sweep];
  11. if (prior > 0) {
  12. for( int j = 1; j <= 9; ++j ) {
  13. int k = sweep * 10 + j; // <-- the key, generating number from digits is much faster than decomposing number into digits
  14. if (k > max) return results;
  15. results[k] = prior * j;
  16. // loop invariant: all values up to k are filled in
  17. }
  18. }
  19. ++sweep;
  20. }
  21. }
  22.  
  23. public static void Main()
  24. {
  25. int[] products = GenerateDigitProducts(99);
  26.  
  27. for( int n = 21; n <= 99; ++n ) {
  28. System.Console.WriteLine(n.ToString("D2") + " => " + products[n].ToString("D3"));
  29. }
  30. }
  31. }
Success #stdin #stdout 0.03s 33816KB
stdin
Standard input is empty
stdout
21 => 002
22 => 004
23 => 006
24 => 008
25 => 010
26 => 012
27 => 014
28 => 016
29 => 018
30 => 000
31 => 003
32 => 006
33 => 009
34 => 012
35 => 015
36 => 018
37 => 021
38 => 024
39 => 027
40 => 000
41 => 004
42 => 008
43 => 012
44 => 016
45 => 020
46 => 024
47 => 028
48 => 032
49 => 036
50 => 000
51 => 005
52 => 010
53 => 015
54 => 020
55 => 025
56 => 030
57 => 035
58 => 040
59 => 045
60 => 000
61 => 006
62 => 012
63 => 018
64 => 024
65 => 030
66 => 036
67 => 042
68 => 048
69 => 054
70 => 000
71 => 007
72 => 014
73 => 021
74 => 028
75 => 035
76 => 042
77 => 049
78 => 056
79 => 063
80 => 000
81 => 008
82 => 016
83 => 024
84 => 032
85 => 040
86 => 048
87 => 056
88 => 064
89 => 072
90 => 000
91 => 009
92 => 018
93 => 027
94 => 036
95 => 045
96 => 054
97 => 063
98 => 072
99 => 081