Dictionary<int, int> nexts = new Dictionary<int, int>(); for (int i = 1; i < 1000000; i++) { int key = i; intvalue = GetNext(i); nexts.Add(key, value);
while (value > 1000000) { key = value; value = GetNext(key); nexts[key] = value; } }
for (int i = 3; i < 1000000; i++) { HashSet<int> numbers = new HashSet<int>(); int cur = i; while (!numbers.Contains(cur)) { numbers.Add(cur); cur = nexts[cur]; }
if (numbers.Count == 60) { count++; } }
return count;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
privatestaticintGetNext(int i) { intvalue = i.ToString().Select(c => { int m = int.Parse(c.ToString()); int product = 1; for (int j = 2; j <= m; j++) { product *= j; } return product; }).Sum();