EularProject 74:Digit factorial chains
来源:互联网 发布:东华软件金融部 编辑:程序博客网 时间:2024/05/20 06:53
Andrew zhang
Sep 4, 2017
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?
Answer:
402
Completed on Mon, 4 Sep 2017, 23:15
Code:
#include <iostream>#include <set>#include <vector>using namespace std;#define SIZE 2177281int table[10];vector<set<int>> state(SIZE);int next_num(int current){ int result = 0; while (current) { result += table[current % 10]; current /= 10; } return result;}void func(int start){ int next = next_num(start); if (next == start) { state[next].insert(next); } if (state[next].size() == 0) { func(next); } state[start] = state[next]; state[start].insert(start);}int main(){ int result = 0; table[0] = 1; for (int i = 1; i < 10; i++) { table[i] = table[i - 1] * i; } state[145].insert(145); state[169].insert(169); state[169].insert(363601); state[169].insert(1454); state[871].insert(871); state[871].insert(45361); state[872].insert(872); state[872].insert(45362); for (int i = 1; i < 1000000; i++) { func(i); if (i % 10000 == 0) cout << i << endl; } for (int i = 1; i < 1000000; i++) { if(state[i].size() == 60) result++; } cout << result << endl; getchar(); return 0;}
- EularProject 74:Digit factorial chains
- Project Euler:Problem 74 Digit factorial chains
- [projecteuler]Factorial digit sum
- Problem 20:Factorial digit sum
- PE 20 Factorial digit sum
- ProjectEuler-Problem20-Factorial digit sum
- Last Digit By Factorial Divide
- 2014 图灵杯 问题 C: Digit Chains
- Project Euler:Problem 92 Square digit chains
- Project Euler Problem 92 Square digit chains
- Project Euler 题解 #20 Factorial digit sum
- projecteuler---->problem=20----Factorial digit sum
- Project Euler:Problem 20 Factorial digit sum
- 20 Factorial digit sum - Project Euler
- Project Euler Problem 20 Factorial digit sum
- project euler 20 Factorial digit sum
- Project Euler 20 Factorial digit sum(大数乘法)
- 欧拉项目 第20题 Factorial digit sum
- linux常用命令(logout,alias,clear,history,wc)
- Activemq实例
- 运行Tomcat时报java.lang.UnsupportedClassVersionError: oracle/demo/oow/bd/ui/IsUserLogued
- Android控制之ToggleButton和Switch与RatingBar
- Swift
- EularProject 74:Digit factorial chains
- python 记录,防止遗忘
- iframe有哪些缺点?应该怎么合理使用?
- Mac电脑上配置Java开发环境
- log4js 日志模块
- 达夫设备(Duff`s Device)减少迭代次数
- Angular CLI 使用教程指南参考
- LeetCode——105. Construct Binary Tree from Preorder and Inorder Traversal
- 反转单链表