DECLARE FUNCTION sod# (n#, b#)
DEFDBL A-Z
CLS
PRINT "base 10"
FOR x = 1 TO 999999
x2 = x * x
IF sod(x2, 10) = x + 2 THEN PRINT x
IF 9 * (LEN(STR$(x2)) + 1) < x THEN EXIT FOR
NEXT
PRINT "stopped at x="; x; "; x^2 ="; x2
DIM SHARED baserep$
PRINT
PRINT "base 11"
FOR x = 1 TO 999999
x2 = x * x
sd = sod(x2, 11): sq$ = baserep$
IF sd = x + 2 THEN
PRINT x;
throwaway = sod(x, 11): PRINT " "; baserep$; TAB(18); x2; sq$
END IF
IF 10 * (LEN(sq$) + 2) < x THEN EXIT FOR
NEXT
PRINT "stopped at x="; x; "; x^2 ="; x2
throwaway = sod(x, 11): PRINT " "; baserep$;
throwaway = sod(x2, 11): PRINT " "; baserep$: PRINT
FUNCTION sod (n, b)
' sum of digits in n (a numeric value), when represented in base b)
c = n
tot = 0
baserep$ = ""
DO
q = INT(c / b)
dig = c - q * b: c = q
baserep$ = MID$("0123456789ABCDEF", dig + 1, 1) + baserep$
tot = tot + dig
LOOP UNTIL c = 0
sod = tot
END FUNCTION
finds
base 10
2
5
8
14
17
stopped at x= 55 ; x^2 = 3025
base 11
2 2 4 4
4 4 16 15
7 7 49 45
9 9 81 74
14 13 196 169
19 18 361 2A9
stopped at x= 61 ; x^2 = 3721
56 2883
Explanation:
The base 10 is fairly self-explanatory: there are 5 values of x that work, the largest, to take one example, being 17. 17^2 = 289, the sum of whose digits is 19, which is 2 larger than 17 so that 19 - 17 = 2.
For base 11 more detail is provided: The left two columns show x, while the right to columns show x^2. Within each pair of columns, the left number is shown in base 10 and the right hand number is shown in base 11. The highest valid x is 18(base 11), whose square is 2A9(base 11), the sum of whose digits is 21(decimal) or 1A(base 11), which is indeed 2 higher than 18(base 11).
In each base, the search was stopped when x exceeded the highest possible sod (all 9's or all A's, respectively) for numbers two digits longer than the square of x. The extra two digits is more than enough to allow for both the difference of 2, and the irregularities in the ups and downs of sod's, considering that the highest possible digit was used in each calculation.
The base-11 search was stopped at x=61(decimal) or 56(base 11), when x^2 = 3721(decimal) or 2883(base 11), as sod(AAAAAA(base 11) ) = 60(decimal).
|
Posted by Charlie
on 2010-09-27 16:05:02 |