题目链接: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. C# 解析XML格式的字符串

    public CreateOrderReturnResult GetCreateOrderReturnApi() { var result = new CreateOrderReturnResult( ...

  2. phpExcel导出excel的类,每步都有说明

    require_once WEB_PATH . '/lib/PHPExcel/PHPExcel.php'; require_once WEB_PATH . '/lib/PHPExcel/PHPExce ...

  3. Windows 8.1 (64bit) 下搭建 Scrapy 0.22 环境

    我的Windows 8.1 环境 1.下载安装Python 2.7.6 在Python官方网站中下载Python2.7.6的Windows安装包,根据默认配置安装到C:\Python27目录. 安装完 ...

  4. ASP.NET中利用Split实现对Checkbox的字符串分离放到DataTable里面

    一.背景 昨天唐欢问了我一个问题: 现在有一个CheckBox和一个Label如下图: 要实现选中CheckBox,点击下面打印按钮的时候要做成这个样子的如下图: 简单的说就是档案编号作为表中的一个列 ...

  5. ubuntu 下dbus的环境搭建和使用

    从https://launchpad.net/ubuntu/+source/dbus/1.10.6-1ubuntu2下载需要的dbus包,然后解压,./configure make && ...

  6. 【js】正则表达式豁然开朗

    http://www.jikexueyuan.com/course/809_3.html?ss=1 小括号,中括号 中括号:[] ,它表示里面的字符任选一个 比如[abcd]+,就表示abcd这四个字 ...

  7. 在后台直接调用sql

    表:FmCashStatistics EFContext db = new EFContext(); string strsql = "SELECT org_info_id,customer ...

  8. TCP相关知识

    1. TCP与TCP/IP协议族 TCP是TCP/IP协议族中运输层的一个协议.TCP/IP,即传输控制协议/网间协议,是一个工业标准的协议集,包含了运输层.网络层和链路层的协议,其结构如下图所示:其 ...

  9. 我的cnblogs设置代码

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...

  10. Ubuntu实用快捷键

    ALT + TAB: 切换程序窗口Win+w: 显示所有的工作空间,可轻松进行切换 ===== Terminal终端 ===== CTRL + ALT + T: 打开终端 TAB: 自动补全命令或文件 ...