(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?
题目大意:
数字145有一个著名的性质:其所有位上数字的阶乘和等于它本身。
1! + 4! + 5! = 1 + 24 + 120 = 145
169不像145那么有名,但是169可以产生最长的能够连接回它自己的数字链。事实证明一共有三条这样的链:
169 363601
1454
169 871
45361
871 872
45362
872
不难证明每一个数字最终都将陷入一个循环。例如:
69 363600
1454
169
363601 (
1454) 78
45360
871
45361 (
871) 540
145 (
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的更多相关文章
- (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 ...
- (Problem 33)Digit canceling fractions
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplif ...
- (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 ...
- (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 ...
- (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 ...
- (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 ...
- (Problem 70)Totient permutation
Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...
- (Problem 46)Goldbach's other conjecture
It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a ...
- (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 ...
随机推荐
- org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x0) was found in the CDATA sectio
偶尔有一次beyond compare比较部署文件时,发现有一个JSP文件结尾的地方有一大堆空白的二进制符号,当时没有管,就覆盖上去了. =================背景分割线========= ...
- Android TXT文件读写
package com.wirelessqa.helper; import java.io.FileInputStream; import java.io.FileOutputStream; impo ...
- input 输入验证
js验证输入框内容 只能输入英文 只能输入英文 无法粘贴,右键不会弹出粘贴菜单 只能输入数字: 只能输入数字,小数点: 只能输入数字,小数点,下划线: 只能输入英文和数字: 只能输入汉字: 禁止输入法 ...
- Convert Sorted List to Balanced Binary Search Tree (BST)
(http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html) Given a singly linked list ...
- python打包成exe
目前有三种方法可以实现python打包成exe,分别为 py2exe Pyinstaller cx_Freeze 其中没有一个是完美的 1.py2exe的话不支持egg类型的python库 2.Pyi ...
- DataGridView插入一行数据和用DataTable绑定数据2种方式
以前不会用DataGridView的时候一直使用DataTable绑定的方式,如下: DataTable table = new DataTable(); //给表添加一列Name,此名字和 tabl ...
- LINQ to Entity Framework 操作符(转)
在开始了解LINQ to Entities之前,需要先对.NET Framework 3.5版本后对C#语言的几个扩展特性做一些阐释,这有助于我们更容易.更深刻的理解LINQ to Entities技 ...
- oracle 数据库数据迁移解决方案
大部分系统由于平台和版本的原因,做的是逻辑迁移,少部分做的是物理迁移,接下来把心得与大家分享一下 去年年底做了不少系统的数据迁移,大部分系统由于平台和版本的原因,做的是逻辑迁移,少部分做的是物理迁 ...
- sql2012管理
一.还原完整备份的语法如下: RESTORE DATABASE { database_name | @database_name_var } --数据库名 [ FRO ...
- D3.js学习记录 - 数据类型【转】【新】
1.变量 JAVASCRIPT的变量是一种类型宽松的语言.定义变量不用指定数据类型.而且还是动态可变的. var value = 100;value = 99.9999;value = false;v ...