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