题目链接: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的更多相关文章

  1. (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 ...

  2. 欧拉工程第69题:Totient maximum

    题目链接 欧拉函数φ(n)(有时也叫做phi函数)可以用来计算小于n 的数字中与n互质的数字的个数. 当n小于1,000,000时候,n/φ(n)最大值时候的n. 欧拉函数维基百科链接 这里的是p是n ...

  3. 欧拉工程第70题:Totient permutation

    题目链接 和上面几题差不多的 Euler's Totient function, φ(n) [sometimes called the phi function]:小于等于n的数并且和n是互质的数的个 ...

  4. 欧拉工程第56题:Powerful digit sum

    题目链接   Java程序 package projecteuler51to60; import java.math.BigInteger; import java.util.Iterator; im ...

  5. 欧拉工程第51题:Prime digit replacements

    题目链接 题目: 通过置换*3的第一位得到的9个数中,有六个是质数:13,23,43,53,73和83. 通过用同样的数字置换56**3的第三位和第四位,这个五位数是第一个能够得到七个质数的数字,得到 ...

  6. 欧拉工程第63题:Powerful digit counts

    题目链接 The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=8 ...

  7. 欧拉工程第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 ...

  8. 欧拉工程第66题:Diophantine equation

    题目链接 脑补知识:佩尔方差 上面说的貌似很明白,最小的i,对应最小的解 然而我理解成,一个循环的解了,然后就是搞不对,后来,仔细看+手工推导发现了问题.i从0开始变量,知道第一个满足等式的解就是最小 ...

  9. 欧拉工程第65题:Convergents of e

    题目链接 现在做这个题目真是千万只草泥马在心中路过 这个与上面一题差不多 这个题目是求e的第100个分数表达式中分子的各位数之和 What is most surprising is that the ...

随机推荐

  1. javascript中的光标

    最近项目中要做一个键盘操作,光标移动的功能:增强用户体验:问朋友查资料了解到这方面的知识:整理备忘: 1.IE使用textRange对象,其他使用selectionStart selectionEnd ...

  2. php更新修改excel中的内容例子

    代码如下 复制代码 //模板存放目录$dir = $DOCUMENT_ROOT.'/backoffice/admin/oemcheck/';  $templateName = '1.xlsx';$ou ...

  3. Android L Ripple的使用

    声明:Demo并不是有本人所写,本人只是总结在这里 工程源码: RippleDemo.zip ---------------------------------------------------- ...

  4. (转)android底部弹出iOS7风格对话选项框(QQ对话框)--第三方开源--IOS_Dialog_Library

    本文转载于:http://blog.csdn.net/zhangphil/article/details/44940339 完成这个效果的是使用了 IOS_Dialog_Library 下载地址:ht ...

  5. js各类共用方法

    function GetParameterValueByName(parametername) { var reg = new RegExp("(^|&)" + param ...

  6. 第六周 E题 期望.....

    Description Given a dice with n sides, you have to find the expected number of times you have to thr ...

  7. Fat-tree 胖树交换网络

    胖树架构下,网络带宽不收敛 传统的树形网络拓扑中,带宽是逐层收敛的,树根处的网络带宽要远小于各个叶子处所有带宽的总和. 而胖树网络则更像是真实的树,越到树根,枝干越粗,即:从叶子到树根,网络带宽不收敛 ...

  8. ios6 处理内存警告

    iPhone下每个app可用的内存是被限制的,如果一个app使用的内存超过20M,则系统会向该app发送Memory Warning消息.收到此消息后,app必须正确处理,否则可能出错或者出现内存泄露 ...

  9. Unity 优化

    1. 尽量避免每帧处理比如: function Update() { DoSomeThing(); } 可改为每5帧处理一次: function Update() { == ) { DoSomeThi ...

  10. android 自定义ratingbar 图片显示不全的解决方案

    在res/style中自定义评分条: <!-- 自定义评分条 --> <style name="roomRatingBar" parent="@andr ...