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  1  1 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?

题目大意:

通过将一个数各位的平方不断相加,直到遇到已经出现过的数字,可以形成一个数字链。

例如:

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

因此任何到达1或89的数字链都会陷入无限循环。令人惊奇的是,以任何数字开始,最终都会到达1或89。

以一千万以下的数字n开始,有多少个n会到达89?

算法一:常规方法,从2~10000000逐个判断,同时统计结果

#include<stdio.h>   

#define N 10000000

int fun(int n)
{
int t, sum;
sum = ;
while(n) {
t = n % ;
sum += t * t;
n /= ;
}
return sum;
} void solve(void)
{
int i, sum, t;
sum = ;
for(i = ; i < N; i++) {
t = fun(i);
while() {
if(t == ) {
sum++;
break;
} else if(t == ) {
break;
} else {
t = fun(t);
}
}
}
printf("%d\n",sum);
} int main(void)
{
solve();
return ;
}

算法二(优化):使用一个bool型数组,保存每次结果,由于最大的中间数为9999999产生的:9^2*7 = 567,所以bool型数组的大小开到600足够

#include <stdio.h>
#include <stdbool.h> #define N 10000000 bool a[] = {false}; int fun(int n)
{
int t, sum;
sum = ;
while(n) {
t = n % ;
sum += t * t;
n /= ;
}
return sum;
} void solve(void)
{
int i, sum, t, temp;
sum = ;
for(i = ; i < N; i++) {
t = fun(i);
temp = t;
if(a[temp]) {
sum++;
} else {
while() {
t = fun(t);
if(a[t] || t == ) {
a[temp] = true;
sum++;
break;
} else if(t == ) {
break;
} else {
}
}
}
}
printf("%d\n",sum);
} int main(void)
{
solve();
return ;
}
Answer:
8581146

(Problem 92)Square digit chains的更多相关文章

  1. Project Euler 92:Square digit chains C++

    A number chain is created by continuously adding the square of the digits in a number to form a new ...

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

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

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

  4. (Problem 34)Digit factorials

    145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are ...

  5. (Problem 33)Digit canceling fractions

    The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplif ...

  6. Project Euler:Problem 63 Powerful digit counts

    The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...

  7. Porject Euler Problem 6-Sum square difference

    我的做法就是暴力,1+...+n 用前n项和公式就行 1^2+2^2+....+n^2就暴力了 做完后在讨论版发现两个有趣的东西. 一个是 (1+2+3+...+n)^2=(1^3)+(2^3)+(3 ...

  8. lintcode:快乐数

    快乐数 写一个算法来判断一个数是不是"快乐数". 一个数是不是快乐是这么定义的:对于一个正整数,每一次将该数替换为他每个位置上的数字的平方和,然后重复这个过程直到这个数变为1,或是 ...

  9. UVA - 10162 Last Digit

    Description  Problem B.Last Digit  Background Give you a integer number N (1<=n<=2*10100). Ple ...

随机推荐

  1. php7 install memcached extension

    #download source code package from git $ git clone https://github.com/php-memcached-dev/php-memcache ...

  2. jedis入门一

    一.下载Jedis的依赖包jedis-2.1.0.jar,然后将其添加到classpath下面. 1. 定义连接:Redis暂时不要设置登录密码 Jedis jedis = new Jedis(&qu ...

  3. getchar()与EOF

    大师级经典的著作,要字斟句酌的去读,去理解.以前在看K&R的The C Programming Language(Second Edition)中第1.5节的字符输入/输出,很迷惑getcha ...

  4. 3.java.lang.ClassNotFoundException

    指定的类不存在 这里主要考虑一下类的名称和路径是否正确即可,通常都是程序试图通过字符串来加载某个类时可能引发 异常 比如: 调用Class.forName(); 或者调用ClassLoad的finaS ...

  5. SSH Session Recorder

    If you want to record your root ssh session  create a file .bash_profile  . and copy below line by l ...

  6. Linux-手动释放缓存(Buffer、Cache)

    /proc是一个虚拟文件系统,我们可以通过对它的读写操作做为与kernel实体间进行通信的一种手段.也就是说可以通过修改/proc中的文件,来对 当前kernel的行为做出调整.那么我们可以通过调整/ ...

  7. SendMessage的返回值,就是由相应的响应消息函数的返回值(解释的简洁明了)

    SendMessage Return Values The return value specifies the result of the message processing and depend ...

  8. QSqlDatabase的进一步封装(多线程支持+更加简单的操作)——同时支持MySQL, SQL Server和Sqlite

    开发背景: 1.直接用QSqlDatabase我觉得太麻烦了: 2.对于某些数据库,多个线程同时使用一个QSqlDatabase的时候会崩溃: 3.这段时间没什么干货放出来觉得浑身不舒服,就想写一个. ...

  9. iOS 视图跳转

    //跳转 - ( void)present:( id )sender { NSLog ( @"the button,is clicked …" ); // 创建准备跳转的 UIVi ...

  10. poj2328---"right on"进入下一个case的模板(while)

    #include <stdio.h> #include <stdlib.h> #include<string.h> int main() { ]; ,end=; w ...