Project Euler:Problem 74 Digit factorial chains

The number 145 is well known for the property that the sum of the factorial of its digits is equal to 145:

1! + 4! + 5! = 1 + 24 + 120 = 145

Perhaps less well known is 169, in that it produces the longest chain of numbers that link back to 169; it turns out that there are 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 will eventually get stuck in a loop. For example,

69 → 363600 → 1454 → 169 → 363601 (→ 1454)
78 → 45360 → 871 → 45361 (→ 871)
540 → 145 (→ 145)

Starting with 69 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() == 60)
				{
					//cout << count << endl;
					count++;
				}
				break;
			}
		}
	}
	cout << count << endl;
	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值