Project Euler:problem Square digit chains

Source: Internet
Author: User

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

For example,

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

Therefore any chain this arrives at 1 or the become stuck in an endless loop. What are most amazing are that every starting number would eventually arrive at 1 or 89.

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



For a number within 10000000, the sum of the squares of each digit is 9*9*7, that is, the next digit of 9999999 should be 9*9*7

Declares a bool array of size 567, which is marked true when the number finally arrives 89 o'clock


Def nextnum (a):    res=0 while    a!=0:        tmp=a%10        res=res+tmp*tmp        A=A//10    return resres=0num=[ False for I in range (9*9*7+2)]for I in Range (9*9*7+1):    nextn=nextnum (i) while    nextn>i and nextn!=89:        Nextn=nextnum (NEXTN)    if Num[nextn]==true or nextn==89:       res=res+1       num[i]=truefor i in range (9*9*7+ 1,10000000):       if Num[nextnum (i)]==true:           res=res+1print (' res = ', res)


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Project euler:problem Square digit chains

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.