The number 145 is well known for the property, the sum of the factorial of its digits are equal to 145:
1! + 4! + 5! = 1 + 24 + 120 = 145
Perhaps less well known was 169, in that it produces the longest chain of numbers, which link back to 169; It turns out that there is only three such loops that exist:
169→363601→1454→169
871→45361→871
872→45362→872
It is not difficult to prove that every starting number would eventually get stuck in a loop. For example,
69→363600→1454→169→363601 (→1454)
78→45360→871→45361 (→871)
540→145 (→145)
Starting with produces a chain of five non-repeating terms, but the longest non-repeating chain with a starting number Below one million is sixty terms.
How many chains, with a starting number below one million, contain exactly sixty non-repeating terms?
#include <iostream> #include <map>using namespace Std;int factor[10] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880};int factnum (int n) {int res = 0;while (n) {res + = factor[n% 10];n/= 10;} return res;} int main () {int count = 0;for (int i = 1; I <= 1000000; i++) {map<int, int>mp;mp[i]++;int tmp = I;while (true) {tmp = Factnum (TMP), if (Mp.find (tmp) = = Mp.end ()) Mp[tmp]++;else{if (mp.size () = =) {//cout << count << endl;count ++;} Break;}}} cout << Count << endl;system ("pause"); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Project Euler:problem Digit factorial chains