欧拉工程第74题:Digit factorial chains
题目链接:https://projecteuler.net/problem=74
数字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个不重复项?
import java.util.TreeSet;
public class P74{
void run(){
int max_n=1000000;
TreeSet<Long> ts = new TreeSet<Long>();
int count=0;
long num=0;
int len=0;
for(int i=69;i<max_n;i++){
ts.clear();
num = i;
len = 0;
while(ts.add(num)==true){
len++;
num=digitFact(num);
}
if(len ==60)
count++;
}
System.out.println(count);
}
// 402
// 13s9ms
long digitFact(long num){
long result = 0;
while(num!=0){
result += Factorial(num%10);
num/=10;
}
return result;
}
long Factorial(long l){
long fact = 1;
for(int i =1;i<=l;i++)
fact*=i;
return fact;
}
public static void main(String[] args){
long t0 = System.currentTimeMillis();
new P74().run();
long t1= System.currentTimeMillis();
System.out.println((t1-t0)/1000+"s"+(t1-t0)%1000+"ms");
}
}
欧拉工程第74题:Digit factorial chains的更多相关文章
- (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 t ...
- 欧拉工程第69题:Totient maximum
题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数维基百科链接 这里的是p是n ...
- 欧拉工程第70题:Totient permutation
题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...
- 欧拉工程第56题:Powerful digit sum
题目链接 Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...
- 欧拉工程第51题:Prime digit replacements
题目链接 题目: 通过置换*3的第一位得到的9个数中,有六个是质数:13,23,43,53,73和83. 通过用同样的数字置换56**3的第三位和第四位,这个五位数是第一个能够得到七个质数的数字,得到 ...
- 欧拉工程第63题:Powerful digit counts
题目链接 The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=8 ...
- 欧拉工程第67题:Maximum path sum II
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the ma ...
- 欧拉工程第66题:Diophantine equation
题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...
- 欧拉工程第65题:Convergents of e
题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...
随机推荐
- 优化过的redis封装类
转http://www.cnblogs.com/jackluo/p/3410192.html <?php /** * RedisCluster 群redius操作类 * * //创建连接 * $ ...
- mongodb持久化
先上一张图(根据此处重画),看完下面的内容应该可以理解. mongodb使用内存映射的方式来访问和修改数据库文件,内存由操作系统来管理.开启journal的情况,数据文件映射到内存2个view:pri ...
- JS遇到的问题解决
1.input 里使用onclick事件,整整花了1个半小时,onclick里面直接用Location.href不需要加<script></script> <?php / ...
- 左右滑动删除ListView条目Item--第三方开源--SwipeToDismiss
Android的SwipeToDismiss是github上一个第三方开源框架(github上的项目链接地址:https://github.com/romannurik/Android-SwipeTo ...
- About Curah
相信下列场景对您来说一点都不陌生:您遇到一个问题,花了好几个小时在网上搜寻解答和可靠的技术内容.即使前往许多技术博客和论坛翻箱倒柜后,还是无法确定要相信谁,也不知道该选哪个答案. Curah! 网站就 ...
- C#——中文转化成拼音
在KS系统中用到了中文转化成拼音的功能.通过查阅资料为下面是代码. /// <summary> /// MyConvert 的摘要说明 /// </summary> publi ...
- Feature Stapling in SharePoint 2010
http://msdn.microsoft.com/en-us/library/bb861862(v=office.12).aspx http://gallery.technet.microsoft. ...
- How to tune SharePoint 2010 Server for better performance?
http://social.technet.microsoft.com/wiki/contents/articles/7926.sharepoint-2010-tips-for-dealing-wit ...
- 常用的机器学习&数据挖掘知识点【转】
转自: [基础]常用的机器学习&数据挖掘知识点 Basis(基础): MSE(Mean Square Error 均方误差),LMS(LeastMean Square 最小均方),LSM(Le ...
- MYSQL存储过程实现in传入参数 where in('1','2')
android 服务器端开发中遇到这么一个问题: 突然发现将字符串传入到存储过程,参数为 '1','2' ,竟然执行无效 所以看到网上有在存储过程中直接拼凑sql的代码,今天也试了一下,可以执行了, ...