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

本题求得是数字 的数位平方和为89 的个数。

很简单,暴力大法好。

剪枝是必要的,要不会吃不消。

数字末为89的链下处简称89链

一条数链中 的数字无非是新数字和 已出现过的数字,89链标记已出现过的数字。

在求链过程中,出现已标记的数字,那么该链必定可到达89。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int q(int x)
{
    return x*x;
}
int f(int x)
{
    int s=0;
    while(x>0)
    {
        s+=q(x%10);
        x/=10;
    }
    return s;
}
int vis[10000000];
int main()
{
    int cnt=0;
    for(int i=2;i<10000000;i++)
    {
        int temp=0;
        int c=i;
        while(c!=1)
        {
            if(c==89||vis[c]){
                temp=1;
                cnt++;
                break;
            }
            c=f(c);

        }
        if(temp==1) //如是89链,则标记数字
        {
            int c=i;
            while(c!=1&&!vis[c])
            {
                if(c==89){
                    temp=1;
                    break;
                }
                c=f(c);
                vis[c]=1;
 
            }
        }
    }

    cout<<cnt<<endl;
}

  

时间: 2024-07-28 23:15:41

Project Euler 92:Square digit chains C++的相关文章

Project Euler:Problem 92 Square digit chains

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

Project Euler 92

0.57s, import itertools import time def conquer(): ans = 0 DIGIT_LIMIT = 7 ITER_STR = "0123456789" sum_square = lambda ss: sum( int( s ) ** 2 for s in str( ss ) ) fact = lambda num: reduce( lambda x, y: x * y, xrange( 1, num + 1 ) ) combinations

Project Euler #80: Square root digital expansion

1 from decimal import getcontext, Decimal 2 3 4 def main(): 5 n = int(raw_input()) 6 p = int(raw_input()) 7 8 getcontext().prec = p+10 # 扩大精度,保证接过 9 sum = 0 10 11 for i in range(1,n+1): 12 nTemp = Decimal(i).sqrt() 13 if nTemp._isinteger() : # 自生函数的判

Python练习题 047:Project Euler 020:阶乘结果各数字之和

本题来自 Project Euler 第20题:https://projecteuler.net/problem=20 ''' Project Euler: Problem 20: Factorial digit sum n! means n × (n ? 1) × ... × 3 × 2 × 1 For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800, and the sum of the digits in the number 10! i

Python练习题 034:Project Euler 006:和平方与平方和之差

本题来自 Project Euler 第6题:https://projecteuler.net/problem=6 # Project Euler: Problem 6: Sum square difference # The sum of the squares of the first ten natural numbers is, # 1**2 + 2**2 + ... + 10**2 = 385 # The square of the sum of the first ten natur

Project Euler:Problem 46 Goldbach&#39;s other conjecture

It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2×12 15 = 7 + 2×22 21 = 3 + 2×32 25 = 7 + 2×32 27 = 19 + 2×22 33 = 31 + 2×12 It turns out that the conjecture was f

Project Euler:Problem 40 Champernowne&#39;s constant

An irrational decimal fraction is created by concatenating the positive integers: 0.123456789101112131415161718192021... It can be seen that the 12th digit of the fractional part is 1. If dn represents the nth digit of the fractional part, find the v

Problem 43 // Project Euler

Sub-string divisibility The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property. Let d1 be the 1st digit, d2 be the

Python练习题 048:Project Euler 021:10000以内所有亲和数之和

本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable numbers Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b