' Puzzlet #025.
' An illegal but occasionally effective
' method of cancelling fractions is to
' cancel like digits. By this method,
' 16/64 = 1/4 because the 6's cancel, and
' the answer is right. Unfortunately, the
' method usually gives wrong answers, as
' 12/24 = 1/4!
' Find all instances where the numerator
' and denominator are different and both
' comprise just 2 digits which give the
' correct answer by this method.
' Ignore trivial results such as 10/40 = 1/4 etc.
' By Dave Ellis.
declare loadArrays()
declare getNewFraction()
def
flag,
i, j, p, q: int
def newFraction: int
def array1[3], array2[3], array3[3]: int
openconsole
print "Calculating ... ": print
i = 11: 'numerator iterator
do
j = i + 1: 'denominator iterator
do
loadArrays()
p = 1
flag = 0
do
q = 1
do
' anything to cancel?
if array1[p] =
array2[q]
array1[p] = 0
array2[q] = 0
flag = 1
endif
q = q + 1
until flag | q > 2
p = p + 1
until flag | p > 2
if
flag: 'means something to cancel
getNewFraction(): 'cancel it
' does cancellation give right ans?
if i * array3[2] = j * array3[1]
print i, "/ ", j, "=
",
print array3[1], "/ ",
array3[2]
endif
endif
j = j + 1
if
j %
10 = 0: 'prevents last denominator digit = 0
j = j + 1
endif
until j > 99
i = i + 1
if i % 10 = 0: 'prevents last numerator digit = 0
i = i + 1
endif
until i > 98
print: print "That's
all!"
print: print
"Press a key to close ..."
do: until inkey$
<> ""
closeconsole
end
sub loadArrays
' splits numerator and denominator up
' into individual digits and loads them
' into array1 (num) and array2 (den).
array1[1] = int(i/10)
array1[2] = int(i % 10)
array2[1] = int(j/10)
array2[2] = int(j % 10)
return
sub getNewFraction()
' The non-zero digit in array1 is copied
' into array3[1] and the non-zero digit in
' array2 is copied into array3[2].
def flag, it: int
it = 1
flag = 0
' process array1
do
if array1[it] > 0
flag = -1
array3[1] = array1[it]
else
it = it + 1
endif
until flag | it > 2
it = 1
flag = 0
' process array2
do
if array2[it] > 1
flag = -1
array3[2] = array2[it]
else
it = it + 1
endif
until flag | it > 2
return