欧拉工程第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 ...
随机推荐
- Mongodb 级联删除查询操作
ObjRelationPojo表一条记录 public class YpObjRelationPojo implements Serializable { @Id private String id; ...
- AJAX局部更新演出排期
<script language="javascript" type="text/javascript"> function createXMLHt ...
- mysql时间处理
两种方式,一个是在数据库查询的时候就截取,另一个就是在使用的时候截取. 1.数据库 select date_format(日期字段,’%Y-%m-%d’) as ‘日期’ from test 2.ja ...
- 1101. Quick Sort (25)
There is a classical process named partition in the famous quick sort algorithm. In this process we ...
- SQLIO Disk Subsystem Benchmark Tool
C:\Program Files (x86)\SQLIO>sqlio -? sqlio v1.5.SG -?: invalid option Usage: sqlio [options] [&l ...
- 【转载】mysqldump的single-transaction和master-data
原文地址:mysqldump的single-transaction和master-data 作者:myownstars 先看一下--lock-tables和--lock-all-tables --lo ...
- Oracle物理的体系结构
体系结构图的学习: 老余服装店的故事 结构图: SQL查询语句 SGA 共享池shared pool 数据缓存区Buffer cache PGA 进程 SQL更新语句 SGA: 日志缓存区 日志文件 ...
- c++各种排序
1.插入排序 void InsertSort(int a[], int n) { int temp, i, j; ; i < n; i++) { ]) { temp = a[i]; ; j &g ...
- 使用OPTIMIZE TABLE命令来整理表碎片实践
操作环境:ubuntu 14.10 mysql 5.6.25 对含有BLOB或TEXT字段的表,若经常做修改或删除类的操作,需要定期执行OPTIMIZE TABLE命令来整理碎片. 1.creat ...
- NodeJS下访问SQL Server
1.下载node-sqlserver (1)msnodesql (msnodesql-0.2.1-v0.8-x64.msi)下载地址:下载 自行选择与自己系统相符的版本,点击安装. (2)msnod ...