(Problem 74)Digit factorial chains

时间:2021-12-17 21:18:54

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 (Problem 74)Digit factorial chains 363601 (Problem 74)Digit factorial chains 1454 (Problem 74)Digit factorial chains 169 871 (Problem 74)Digit factorial chains 45361 (Problem 74)Digit factorial chains 871 872 (Problem 74)Digit factorial chains 45362 (Problem 74)Digit factorial chains 872

It is not difficult to prove that EVERY starting number will eventually get stuck in a loop. For example,

69 (Problem 74)Digit factorial chains 363600 (Problem 74)Digit factorial chains 1454 (Problem 74)Digit factorial chains 169 (Problem 74)Digit factorial chains 363601 ((Problem 74)Digit factorial chains 1454) 78 (Problem 74)Digit factorial chains 45360 (Problem 74)Digit factorial chains 871 (Problem 74)Digit factorial chains 45361 ((Problem 74)Digit factorial chains 871) 540 (Problem 74)Digit factorial chains 145 ((Problem 74)Digit factorial chains 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?

题目大意:

数字145有一个著名的性质:其所有位上数字的阶乘和等于它本身。

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

169不像145那么有名,但是169可以产生最长的能够连接回它自己的数字链。事实证明一共有三条这样的链:

169 (Problem 74)Digit factorial chains 363601 (Problem 74)Digit factorial chains 1454 (Problem 74)Digit factorial chains 169 871 (Problem 74)Digit factorial chains 45361 (Problem 74)Digit factorial chains 871 872 (Problem 74)Digit factorial chains 45362 (Problem 74)Digit factorial chains 872

不难证明每一个数字最终都将陷入一个循环。例如:

69 (Problem 74)Digit factorial chains 363600 (Problem 74)Digit factorial chains 1454 (Problem 74)Digit factorial chains 169 (Problem 74)Digit factorial chains 363601 ((Problem 74)Digit factorial chains 1454) 78 (Problem 74)Digit factorial chains 45360 (Problem 74)Digit factorial chains 871 (Problem 74)Digit factorial chains 45361 ((Problem 74)Digit factorial chains 871) 540 (Problem 74)Digit factorial chains 145 ((Problem 74)Digit factorial chains 145)

从69开始可以产生一条有5个不重复元素的链,但是以一百万以下的数开始,能够产生的最长的不重复链包含60个项。

一共有多少条以一百万以下的数开始的链包含60个不重复项?

//(Problem 74)Digit factorial chains
// Completed on Tue, 18 Feb 2014, 04:21
// Language: C11
//
// 版权所有(C)acutus (mail: acutus@126.com)
// 博客地址:http://www.cnblogs.com/acutus/
#include<stdio.h>
#include<math.h>
#include<stdbool.h> #define N 1000000
long long fac[]; //保存1~ 9阶乘的数组 long long factorial(int n) //计算阶乘函数
{
if(n == || n == ) return ;
else return n * factorial(n - );
} void init() //初始化数组
{
int i;
for(i = ; i <= ; i++) {
fac[i] = factorial(i);
}
} long long sum(long long n) //计算整数n各位的阶乘的和
{
int ans = ;
while(n) {
ans += fac[n % ];
n /= ;
}
return ans;
} bool fun(int n)
{
int i, count, t;
bool flag = false;
count = ;
while() {
switch(n) {
case : count += ; flag = true; break;
case : count += ; flag = true; break;
case : count += ; flag = true; break;
case : count += ; flag = true; break;
case : count += ; flag = true; break;
case : count += ; flag = true; break;
case : count += ; flag = true; break;
default: t = sum(n);
if( n == t) {
flag = true;
break;
} else{
n = t;
count++; break;
}
}
if(flag) break;
}
if(count == ) return true;
else return false;
} void solve()
{
int i, count;
count = ;
for(i = ; i <= N; i++) {
if(fun(i)) count++;
}
printf("%d\n", count);
} int main()
{
init();
solve();
return ;
}
Answer:
402

(Problem 74)Digit factorial chains的更多相关文章

  1. (Problem 34)Digit factorials

    145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are ...

  2. (Problem 33)Digit canceling fractions

    The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplif ...

  3. (Problem 16)Power digit sum

    215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of th ...

  4. (Problem 73)Counting fractions in a range

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

  5. (Problem 42)Coded triangle numbers

    The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...

  6. (Problem 41)Pandigital prime

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...

  7. (Problem 70)Totient permutation

    Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...

  8. (Problem 46)Goldbach&&num;39&semi;s other conjecture

    It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a ...

  9. (Problem 72)Counting fractions

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

随机推荐

  1. H5 Notes:Navigator Geolocation

    H5的地理位置API可以帮助我们来获取用户的地理位置,经纬度.海拔等,因此我们可以利用该API做天气应用.地图服务等. Geolocation对象是我们获取地理位置用到的对象. 首先判断浏览器是否支持 ...

  2. Shiro安全框架入门篇(登录验证实例详解与源码)

    转载自http://blog.csdn.net/u013142781 一.Shiro框架简单介绍 Apache Shiro是Java的一个安全框架,旨在简化身份验证和授权.Shiro在JavaSE和J ...

  3. 【ufldl tutorial】Convolution and Pooling

    卷积的实现: 对于每幅图像,每个filter,首先从W中取出对应的filter: filter = squeeze(W(:,:,filterNum)); 接下来startercode里面将filter ...

  4. Noip2015总结

    Noip2015战役总结 [游记部分] Day0 考前说是可以放松一下,下午呢就在机房打了几盘杀,一起玩了玩狼人.不过晚上觉得还是要有点氛围了,于是稍稍打了几个模板,觉得正确率还不错,给自己一点自信的 ...

  5. Hbase实例

    import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.had ...

  6. WPF 自定义路由事件 与 附加路由事件

    为student添加附件事件

  7. code&colon;blocks 编译环境设置

    1.  支持C99 在菜单settings->compiler settings->comiler settings->Other options 添加: -std=c99 2. 支 ...

  8. javascript函数的基础功能

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  9. POJ1458 Common Subsequence 【最长公共子序列】

    Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37614   Accepted: 15 ...

  10. Flask入门之完整项目搭建

    一.创建虚拟环境 1,新建虚拟环境 cmd中输入:mkvirtualenv 环境名 2,在虚拟环境安装项目运行所需要的基本模块 pip install flask==0.12.4 pip instal ...