Determine all possible values of a base ten positive integer N such that N is precisely one
more than the sum of the squares of its digits.
We need not check farther than 3-digit numbers, as 4 * 9^2 = 324 -- only a 3-digit number; and in fact, 3 * 9^2 = 243, so even that number need not be exceeded.
DECLARE FUNCTION sumsq# (x#)
DEFDBL A-Z
FOR n = 1 TO 243
IF n = sumsq(n) + 1 THEN PRINT n
NEXT n
FUNCTION sumsq (x)
s$ = LTRIM$(STR$(x))
t = 0
FOR i = 1 TO LEN(s$)
t = t + VAL(MID$(s$, i, 1)) * VAL(MID$(s$, i, 1))
NEXT
sumsq = t
END FUNCTION
the results being only 35 and 75.
|
Posted by Charlie
on 2011-09-03 14:56:00 |