THE PUZZLET PAGE

PUZZLET 025 - THE SOLUTION
' 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

       
PROGRAM NOTES

Three arrays are set up to handle this problem:

1.  
  array1 holds the numerator of the fraction to be cancelled
2.   
  array2 holds the denominator of the fraction to be cancelled
3.  
  array3 holds the cancelled fraction from arrays 1 and 2

Variables i and j are used to generate every possible combination of fractions where both the nominator and denumerator are two digits each.  Each fraction is loaded into arrays 1 and 2 as indicated above using the subroutine LoadArrays(). The arrays split each 2-digit integer into two single digits to enable comparisons for cancelling to be made.

We are ready now to look for "cancellable" digits.  Variables p and q are used as pointers to facilitate this.  They check each digit in the numerator against both digits in the denominator.  If a match is found, both matching digits are set to 0.

When the code detects this has occurred for a particular fraction, the subroutine GetNewFraction() is called to transfer the non-zero digits of arrays 1 and 2 into array3.  

The main routine now goes on to check if the fraction in array3 is equal to the original fraction by cancellation.

Before going on to investigate the next fraction, the code ensures that neither numerator nor denominator ends in a zero to avoid investigating trivial examples, and also to avoid divide by zero crashes etc.

When you run the code,  it will produce the inset screen.  

There are four solutions in all, and  that is the complete list.


Calculating ...

16 / 64 = 1 / 4
19 / 95 = 1 / 5
26 / 65 = 2 / 5
49 / 98 = 4 / 8

That's all!

Press a key to close ...

          


MAIN MENU
DOWNLOAD CODE
ARCHIVES

Site design/maintenance: Dave Ellis E-mail me!
Last Updated: February 22nd, 2004.