In searching some solutions to a problem in a scientific journal I wrote the
following basic programme and hit some problems with STR$. Please run the code
to see for yourself - I commented the problems (errors???) in the code:
'error in STR$()?
'the code: find 'a' for a^b for which a=sum(digits a^b)
#COMPILE EXE
DEFLNG a-z
FUNCTION PBMAIN () AS LONG
FOR b = 8 TO 9 'exponent (here 8-9 as too many solutions with lower exp.)
FOR a = 2 TO 99 'base number
g&& = a^b 'number
IF g&& < 0 THEN EXIT FOR 'number was bigger than max Quad: 2^63-1
g$ = STR$(g&&,18) 'number as digits with 18 digs precision (STR$ defaults to 16, max is 18)
L = LEN(g$) 'length is digits in the number
s = 0 'intiation for sum of separate digits
FOR i = 1 TO L
s = s + VAL(MID$(g$,i,1)) 'sum of separate digits in the number
NEXT i
IF s = a THEN MSGBOX "a="+STR$(a)+", b="+STR$(b)+", number="+g$
NEXT b
NEXT a
MSGBOX "all found" 'but not 8^63, 9^71 and 9^81
'if g$ = STR$(g&&) is used instead of STR$(g&&,18) then 8^63 is found, but 54^9 is not
p! = 28/9 'this is 3.111111
MSGBOX STR$(p!,18) 'STR$ erroneously adds some other digits
END FUNCTION
It seems correct that 9^81 can not be displayed by STR$ because with the sign it has 19 digits.
I did find a way to correctly display all solutions as follows:
instead of the line "g$ = STR$(g&&,18)" replace it by:
g1&& = g&& \ 1000000000 : g2&& = g&& - g1&&*1000000000 'split large numbers in 2
IF g1&& THEN
g$ = STR$(g1&&) + LTRIM$(STR$(g2&&)) 'LTRIM$ is used to delete the leading (+) sign
ELSE
g$ = STR$(g&&) 'number smaller than 1000000000
END IF
I do not understand the strange behaviour of STR$ and would like to get some explanation.
Maybe this is another something to fix for PB R&D?
Also, it would make sense to accomodate
STR$ for the longest possible number in the next PBDLL release.
Thanks, Jules
------------------
following basic programme and hit some problems with STR$. Please run the code
to see for yourself - I commented the problems (errors???) in the code:
'error in STR$()?
'the code: find 'a' for a^b for which a=sum(digits a^b)
#COMPILE EXE
DEFLNG a-z
FUNCTION PBMAIN () AS LONG
FOR b = 8 TO 9 'exponent (here 8-9 as too many solutions with lower exp.)
FOR a = 2 TO 99 'base number
g&& = a^b 'number
IF g&& < 0 THEN EXIT FOR 'number was bigger than max Quad: 2^63-1
g$ = STR$(g&&,18) 'number as digits with 18 digs precision (STR$ defaults to 16, max is 18)
L = LEN(g$) 'length is digits in the number
s = 0 'intiation for sum of separate digits
FOR i = 1 TO L
s = s + VAL(MID$(g$,i,1)) 'sum of separate digits in the number
NEXT i
IF s = a THEN MSGBOX "a="+STR$(a)+", b="+STR$(b)+", number="+g$
NEXT b
NEXT a
MSGBOX "all found" 'but not 8^63, 9^71 and 9^81
'if g$ = STR$(g&&) is used instead of STR$(g&&,18) then 8^63 is found, but 54^9 is not
p! = 28/9 'this is 3.111111
MSGBOX STR$(p!,18) 'STR$ erroneously adds some other digits
END FUNCTION
It seems correct that 9^81 can not be displayed by STR$ because with the sign it has 19 digits.
I did find a way to correctly display all solutions as follows:

instead of the line "g$ = STR$(g&&,18)" replace it by:
g1&& = g&& \ 1000000000 : g2&& = g&& - g1&&*1000000000 'split large numbers in 2
IF g1&& THEN
g$ = STR$(g1&&) + LTRIM$(STR$(g2&&)) 'LTRIM$ is used to delete the leading (+) sign
ELSE
g$ = STR$(g&&) 'number smaller than 1000000000
END IF
I do not understand the strange behaviour of STR$ and would like to get some explanation.

Maybe this is another something to fix for PB R&D?

STR$ for the longest possible number in the next PBDLL release.
Thanks, Jules
------------------
Comment