Square digit chains

Problem 92

A number chain is created by continuously adding the square of the digits in a number to form a new number until it has been seen before.

For example,

44 → 32 → 13 → 10 → 11
85 → 89 → 145 → 42 → 20 → 4 → 16 → 37 → 58 → 89

Therefore any chain that arrives at 1 or 89 will become stuck in an endless loop. What is most amazing is that EVERY starting number will eventually arrive at 1 or 89.

How many starting numbers below ten million will arrive at 89?

C++(Faster):

#include <iostream>
#include <cstring>
#include <stack>using namespace std;const int MAXN = 10000000;int arrive[MAXN], chainno[MAXN];
int no;int nextval(int n)
{int sum, v;sum = 0;while(n) {v = n % 10;sum += v * v;n /= 10;}return sum;
}void makechain(int n)
{no++;while(n != 1 && n != 89) {if(chainno[n] > 0) {n = arrive[chainno[n]];break;}chainno[n] = no;n = nextval(n);}arrive[no] = n;
}int main()
{int n;while(cin >> n && n <= MAXN) {memset(arrive, 0, sizeof(arrive));memset(chainno, 0, sizeof(chainno));chainno[1] = 1;arrive[1] = 1;chainno[89] = 2;arrive[2] = 89;no = 2;for(int i=1; i<n; i++)if(i != 1 && i != 89)makechain(i);int count = 0;for(int i=1; i<n; i++)if(arrive[chainno[i]] == 89)count++;cout << count << endl;}return 0;
}

C++:

#include <iostream>
#include <cstring>
#include <stack>using namespace std;const int MAXN = 10000000;int arrive[MAXN];int nextval(int n)
{int sum, v;sum = 0;while(n) {v = n % 10;sum += v * v;n /= 10;}return sum;
}void makechain(int n)
{stack<int> s;while(n != 1 && n != 89) {if(arrive[n] > 0) {n = arrive[n];break;}s.push(n);n = nextval(n);}while(!s.empty()) {int val = s.top();s.pop();arrive[val] = n;}
}int main()
{int n;while(cin >> n && n <= MAXN) {memset(arrive, 0, sizeof(arrive));arrive[1] = 1;arrive[89] = 89;for(int i=1; i<n; i++)makechain(i);int count = 0;for(int i=1; i<n; i++)if(arrive[i] == 89)count++;cout << count << endl;}return 0;
}

Project Euler Problem 92 Square digit chains相关推荐

  1. Project Euler 92:Square digit chains 平方数字链

    题目 Square digit chains A number chain is created by continuously adding the square of the digits in ...

  2. Project Euler Problem 66

    Problem 66 Consider quadratic Diophantine equations of the form: x2 – Dy2 = 1 For example, when D=13 ...

  3. Project Euler Problem 104 Pandigital Fibonacci ends

    Pandigital Fibonacci ends Problem 104 The Fibonacci sequence is defined by the recurrence relation: ...

  4. Project Euler Problem 27小结

    Project Euler上有很多有意思的问题,刚做到第27题,对这个问题做个小结. Problem 27: Euler有一个著名的方程n^2+n+41,当n=0到39时,方程结果均为质数.如今人们用 ...

  5. Project Euler Problem 27 Quadratic primes

    Quadratic primes Problem 27 Euler discovered the remarkable quadratic formula: n2+n+41 It turns out ...

  6. Project Euler Problem 25 1000-digit Fibonacci number

    1000-digit Fibonacci number Problem 25 The Fibonacci sequence is defined by the recurrence relation: ...

  7. Project Euler Problem 14 Longest Collatz sequence

    Longest Collatz sequence Problem 14 The following iterative sequence is defined for the set of posit ...

  8. Project Euler Problem 48: Self powers

    Self powers Problem 48 The series, 11 + 22 + 33 + ... + 1010 = 10405071317. Find the last ten digits ...

  9. Project Euler Problem 53: Combinatoric selections【组合数】

    PE其他解题报告请参考这里,本题答案在留言首条 Combinatoric selections Problem 53 There are exactly ten ways of selecting t ...

最新文章

  1. Nginx配置与使用
  2. Linux Container 研究报告
  3. Linux redhat下安装jdk-6u45-linux-x64.bin
  4. wxpython wx listctrl_wxPython - ListCtrl列表排序
  5. 信息学奥赛一本通(1076:正常血压)
  6. SpringBoot+Vue表单文件上传
  7. 关于js拷贝对象的问题
  8. 解决12c安装过程中的各种报错
  9. html整体布局居中,html 居中布局
  10. c语言多字符和宽字符,2.1.5 多字节字符和宽字符
  11. oracle append parallel,大量数据快速插入方法探究[nologging+parallel+append]
  12. [Android]在Android TV中实现组合按键的监听触发功能
  13. 领存Xeon E5 6U VPX高性能计算刀片上市
  14. unity 3d如何隐藏物体
  15. 腾讯离职元老的内部邮件:马化腾的趣事
  16. 修改yum源为国内yum源和本地yum源
  17. Filter、FilterChain、FilterConfig
  18. 【VC】accent conversion 口音转换论文
  19. gradle-6.7-all 快速下载
  20. jzoj4210. 【五校联考1day1】我才不是萝莉控呢(哈夫曼树)

热门文章

  1. Android 利用缓存机制实现文件下载
  2. 编写IOCP时的几个注意事项(Mirosoft)
  3. linux终端 美化 git,linux终端美化oh-my-zsh
  4. mysql和hdfs性能对比,Hadoop vs Spark性能对比 -数据库-火龙果软件工程
  5. centos7 查看oracle运行日志_Linux(CentOS7)部署系列---常规应用部署方案
  6. android 手机工具箱,Android超级工具箱,你的手机可能缺一个!
  7. pytorch: Tensor的创建与调整
  8. flink基本程序架构
  9. 放置奇兵公会副本攻略_放置奇兵维萨配什么魔兽,放置奇兵装备怎么获得-放置奇兵攻略...
  10. 第2章—装配Bean—自动化装配Bean