' Puzzlet #032.
' Is there a positive integer temperature in
' centigrade which can be read correctly as
' fahrenheit by removing the leftmost digit
' and placing it at the end? The following
' example shows the digit transposition, but
' it's not the correct answer: 234 -> 342.
' By Dave Ellis.
def c, f, flag, transC: int
def cStr$, transCStr$: string
c = 15
flag = 0
openconsole
print "Calculating ..."
do
f = 32 + 9*(c/5)
cStr$ = ltrim$(str$(c))
' move left digit to right position
transCStr$ = right$(cStr$, len(cStr$) -1) + left$(Str$,1)
transC = val(transC$)
if transC = f
flag = 1
endif
c = c + 5
until flag
print: print c-5, "degs C = ",
print f, "degs F."
print: print "Press a key to exit ...",
do: until inkey$ <> ""
closeconsole
end
PROGRAM NOTES
The only thing you need to know for this simple Puzzlet is the formula for converting temperature in centigrade to fahrenheit:
f = 32 + 9*(c/5)
This formula divides variable c (degrees centigrade) by 5 each time around the main DO loop. Since we know that variable fc to 5 (the first positive value capable of being divided exactly by 5) and then incrementing by 5 thereafter until the solution is found.
Variable transCStr$ (for transformation of cStr$, itself derived from c) is used to hold the transformed value of c, that is, the value after moving its leftmost digit to the right end. It was originally converted into string format to allow this manipulation to be carried out. Once the transformation is complete, the value can be returned to integer format and stored in variable transC.
All that remains is to compare this value with that already found by the formula and stored in variable f. If they're the same, we have the answer. If they're not the loop is executed again, this time with c's value incremented by 5. If they had been the same, variable flag would be set to trigger an exit from the DO loop.
Once the loop is exited, the solution is printed to the screen and the program terminates. (degrees fahrenheit) is going to be an integer, we can start the search by initialising
When you run the code, it will produce the inset screen. I doubt if it's possible to measure such elevated temperatures to this kind of precision ...
Calculating ...
121955 degs C = 219551 degs F.
Press a key to exit ...
MAIN MENU
DOWNLOAD CODE
ARCHIVES
Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.