Project Euler:problem Digit factorial chains

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.