Contribution for Puzzlet #058

From:  Denzil Newman [Denzil.Newman@BlueYonder.Co.UK]

' Rapid solution by D.Newman

'

' I saw the problem set at
' http://www.puzzlet.co.uk/This_Weeks_Puzzlet.html

' and realised that I could probably make the
' solution very quickly (in fact while dinner was
' cooking), although there are probably better ways
' of doing it. My program loops from 1-9999 then
' creates a string and reverses it to calculate the
' reverse. After this the answer is calculated and
' converted to a string. The length is then compared
' (as if the length does not match it cannot have all
' the numbers in it). Each character is then checked
' to see if it is in the original and not in a string
' called used. If so the character is added to used.
' In this fashion all characters are tested then the
' successful numbers are printed.

const True = -1

const False = not True

def count as int

def subCount as int

def inv1 as string

def inv2 as string

def invAns as int

def invStr as string

def used as string

def found as int

OPENCONSOLE

color 9,15

cls

print "====="

color 1,15

print "Find any integer not greater than 9999 such that subtracting"

print "its reverse from the original yields an anagram of the original."

print

color 12,15

print "======"

color 0,15

for count=1 to 9999

inv1=ltrim\$(str\$(count))

inv2=""

for subCount=1 to len(inv1)

inv2=mid\$(inv1,subCount,1)+inv2

next subCount

invVal=count-val(inv2)

invStr=Ltrim\$(Str\$(invVal))

if len(invStr)=len(inv1)

used=""

found=true

for subCount=1 to len(invStr)

if instr(inv1, mid\$(invStr,subCount,1))

if instr(used, mid\$(invStr,subCount,1))

found=false

else

used=used+mid\$(invStr,subCount,1)

endif

else

found=false

endif

next subCount

if found=true

while len(inv1)<4

inv1=" "+inv1

endwhile

print inv1+" - "+inv2+" = "+invStr

endif

endif

next count

print

color 2,15

print "press a key to continue."

while inkey\$=""

endwhile

CLOSECONSOLE

END

Thanks for the program.  It runs in just a second or so on my machine and produces all possible solutions in a neat screen printout.  The code is clear and easy to follow.  Well done.

Dave.