Re: Mini Challenge from comp.sys.hp48 Message #20 Posted by PeterP on 28 Mar 2010, 5:41 p.m., in response to message #1 by Egan Ford
Having a couple of hours free today I thought I give it a try with MCODE for the 41. Alas, we don't have enough digits precision (10) in the normal format and I'm too lazy right now to see if 13 digits would be enough (as noone has posted the actual solution number). The below MCODE program tests until X=316 before it crashes out without having found a solution. Is that correct? Maybe I get around to rewriting it for 13 digits but looking at the code, its not trivial and run time wise, this is going to be slow as well. But would be great to know how high the solution number is and if it can be found with 13 digit accuracy at all...
For those enjoying MCODE, here is the code:
To speed up the expensive part - calculating x^4, I first calculate which power of 2 is smaller than x (and then smaller than x^2) as the NUT can do multiplication by 2 very quickly so that should speed up the power calculation. It also checks for the starting number not ending in 1,3,5,7,9,0. All of them seem obvious, although it took a little while for me to make sure 5 is correct as well.
Cheers,
Peter
;=============================================================================================
; X4 - Find a number x who's 4th power has no digit less than 5
;
; Takes a starting number from x
; moves into a mantissa only format, right justified
; checks that does not end in 1,3,5,7,9,0
; calcs x^2
; calcs x^4
; Checks if any digits <5
; If no, putputs result to X-reg
; If yes, read x, increase by one, loop
;©pplatzer 2010
;=============================================================================================
.NAME "X4" ;Main Code
*0012 0B4 #0B4 ; "4"
*0013 018 #018 ; "X"
0014 1A0 [X4] A=B=C=0 ;tabula rasa
0015 2A0 SETDEC ;all calcs in decmode
0016 35C R= 12 ;spot of first digit in C[M]
0017 0F8 READ 3(x) ;get start number
0018 2FA ?C#0 M ;check that not 0
0019 06B JNC [StartW1] +13 0026 ;no -> start with 1
001A 2F6 ?C#0 XS ;check that no negative exponent
001B 05F JC [StartW1] +11 0026 ;no -> start with 1
001C 31C R= 1
001D 2E2 ?C#0 @R ;check that exp < 10
001E 047 JC [StartW1] +8 0026 ;no -> start with 1
001F 05E C=0 MS ;just in case
0020 39C R= 0
0021 2A2 C=-C-1 @R ;complement exponent
0022 262 [RJus] C=C-1 @R ;right justify number
0023 02F JC [RJusD] +5 0028 ;if underflow, we are done
0024 3DA RSHFC M
0025 3EB JNC [RJus] -3 0022
0026 04E [StartW1] C=0 ALL ;invalid start number
0027 23A C=C+1 M ;starting point = 1
0028 046 [RJusD] C=0 S&X ;clean up from shifting loop
0029 27A C=C-1 M ;as [NxtTry] does a C=C+1 first
002A 0E8 WRIT 3(x) ;write start to X
002B 1A0 [NxtTry] A=B=C=0 ;tabula rasa
002C 270 RAMSLCT ;select first ram chip
002D 0F8 READ 3(x) ;read last try #
002E 23A C=C+1 M ;next digit
002F 289003 ?CGO [ERROF] 00A2
0031 0E8 WRIT 3(x) ;
0032 01C [CheckDigs] R= 3 ;check that last dig is not 1,3,5,7,9,0
0033 10E A=C ALL ;save number into A
0034 2E2 ?C#0 @R ;check for 0
0035 3B3 JNC [NxtTry] -10 002B
0036 222 C=C+1 @R ;check for 9
0037 3A7 JC [NxtTry] -12 002B
0038 222 C=C+1 @R
0039 222 C=C+1 @R ;check for 7
003A 38F JC [NxtTry] -15 002B
003B 222 C=C+1 @R
003C 222 C=C+1 @R ;check for 5
003D 377 JC [NxtTry] -18 002B
003E 222 C=C+1 @R
003F 222 C=C+1 @R ;check for 3
0040 35F JC [NxtTry] -21 002B
0041 222 C=C+1 @R
0042 222 C=C+1 @R ;check for 1
0043 347 JC [NxtTry] -24 002B
0044 04E [StartX2] C=0 ALL ;good start number in A
0045 09A B=A M ;save number into B
0046 23A C=C+1 M ;C:= 1
0047 1A6 A=A-1 S&X
0048 070 [Get2Pwr] N=C ;save this power to N
0049 166 A=A+1 S&X ;find which power of 2 < #
004A 1FA C=C+C M ;2, 4, 8, 16, ...
004B 31A ?A<C M ;still # smaller than 2^n
004C 3E3 JNC [Get2Pwr] -4 0048 ;yes, keep looping
004D 1A6 A=A-1 S&X ;so that we can jump on 0 underflow
004E 2EF JC [NxtTry] -35 002B ;if underflow, we need to try next number
004F 0B0 C=N ;get back last power of 2 that was smaller thanb #
0050 1DA A=A-C M ;those are the multiplication left after the 2^n
0051 0DA C=B M ;get back #
0052 1FA [DoPwr2] C=C+C M ;start multi
0053 289003 ?CGO [ERROF] 00A2 ;if overflow -> out of range
0055 1A6 A=A-1 S&X ;count down n from 2^n
0056 3E3 JNC [DoPwr2] -4 0052
0057 07A [DoRestM] A<>B M ;get # back
0058 21A C=A+C M ;do rest multi
0059 289003 ?CGO [ERROF] 00A2
005B 07A A<>B M
005C 1BA A=A-1 M
005D 3D3 JNC [DoRestM] -6 0057
005E 07A A<>B M ;correct one to many additions
005F 0BA C<>A M
0060 25A C=A-C M ;correct result for x^2
;------------------------------------ Stepping Stone -----------------------------
0061 013 JNC [StartX4] +2 0063
0062 24B [NxtTryJP] JNC [NxtTry] -55 002B
;------------------------------------ Stepping Stone -----------------------------
0063 046 [StartX4] C=0 S&X ;clean up
0064 10E A=C ALL
0065 08E B=A ALL
0066 04E C=0 ALL ;find power of 2
0067 23A C=C+1 M
0068 1A6 A=A-1 S&X
0069 070 [4Get2Pwr] N=C ;save this power to N
006A 166 A=A+1 S&X ;find which power of 2 < #
006B 1FA C=C+C M ;2, 4, 8, 16, ...
006C 31A ?A<C M ;still # smaller than 2^n
006D 3E3 JNC [4Get2Pwr] -4 0069 ;yes, keep looping
006E 1A6 A=A-1 S&X ;so that we can jump on 0 underflow
006F 39F JC [NxtTryJP] -13 0062 ;if underflow, we need to try next number
0070 0B0 C=N ;get back last power of 2 that was smaller thanb #
0071 1DA A=A-C M ;those are the multiplication left after the 2^n
0072 0DA C=B M ;get back #
0073 1FA [4DoPwr2] C=C+C M ;start multi
0074 289003 ?CGO [ERROF] 00A2 ;if overflow -> out of range
0076 1A6 A=A-1 S&X ;count down n from 2^n
0077 3E3 JNC [4DoPwr2] -4 0073
0078 07A [4DoRestM] A<>B M ;get # back
0079 21A C=A+C M ;do rest multi
007A 289003 ?CGO [ERROF] 00A2
007C 07A A<>B M
007D 1BA A=A-1 M
007E 3D3 JNC [4DoRestM] -6 0078
007F 07A A<>B M ;correct one to many additions
0080 0BA C<>A M
0081 25A C=A-C M ;correct result for x^4
0082 00E [CheckR] A=0 ALL ;now check if good result
0083 0AE A<>C ALL ;# into A
0084 130105 LDIS&X 105 ;check for 5
0086 33C RCR 1 ;5 into C[MS]
0087 056 C=0 XS ; 008 into C[S&X]
0088 3EE [ShftNr] LSHFA ALL ;move # into A[MS]
0089 266 C=C-1 S&X
008A 35E ?A#0 MS ;are we there yet?
008B 3EB JNC [ShftNr] -3 0088
008C 31E [TstDig] ?A<C MS ;is digit < 5
008D 2AF JC [NxtTryJP] -43 0062 ;if yes, try next number
008E 3EE LSHFA ALL ;shift in next digit
008F 266 C=C-1 S&X ;count down number of digits
0090 3E3 JNC [TstDig] -4 008C ;loop to test next digit
0091 1A0 [FoundIt] A=B=C=0 ;if getting here, we found the number
0092 0F8 READ 3(x) ;now move to correct number formnat
0093 35C R= 12
0094 2E2 [FrmtNr] ?C#0 @R
0095 027 JC [X4Done] +4 0099
0096 2FC RCR 13 ; =-1
0097 166 A=A+1 S&X
0098 3E3 JNC [FrmtNr] -4 0094
0099 0A6 [X4Done] A<>C S&X
009A 39C R= 0 ;only exp up to 9 possible anyway
009B 2A2 C=-C-1 @R
009C 0E8 WRIT 3(x)
009D 3E0 RTN
|