tm = TIMER
FOR n = 1 TO 1000000
s$ = STR$(n)
t = 0
FOR i = 1 TO LEN(s$)
v = VAL(MID$(s$, i, 1))
t = t + v * v
NEXT
IF t = ABS(n - 2009) THEN PRINT n
NEXT
PRINT TIMER - tm
finds
1880
2054
12.46875
The first two numbers, 1880 and 2054 are the only two solutions. The 12.46875 is how many seconds it took to go through the values of n through 1,000,000. A million is certainly sufficient as even the sum of the squares of the digits 999999 is only 486--nowhere near 999999-2009--and the n-2009 multiplies tenfold with every increase by 81 of the largest total possible for the squares of the digits for a given number of digits.
The following UBASIC program does the same as the above QB program, but does it faster, so QB isn't so quick.
5 Tm=time1000
10 for N=1 to 1000000
20 S=str(N)
30 T=0
40 for I=1 to len(S)
50 V=val(mid(S,I,1))
60 T=T+V*V
70 next
80 if T=abs(N-2009) then print N
90 next
100 print:print time1000-Tm
1880
2054
2981
The time taken, 2981, is shown in milliseconds, and so is less than 3 seconds.
|
Posted by Charlie
on 2010-05-09 14:16:40 |