A practical tale of hacking with Revolution
Geoff Canyon
gcanyon at inspiredlogic.com
Sun Sep 21 12:45:01 EDT 2003
So, my dad sent me this web site with the question: how does it work?
http://digicc.com/fido/
The site asks you to pick a number,
jumble the digits to make a second number,
subtract the smaller from the larger,
cross out a digit (not 0),
and enter the rest.
It then tells you which number you picked.
For those of you who like to be mystified, or want to figure it out
yourselves, read no further. Others, scroll down (there's transcript at
the end).
Almost there...
A little further...
Okay.
So, I have a head start in figuring this out, because I have a
background in math puzzles from way back, and this is a math puzzle.
The site asks you to choose a 3 or 4 digit number, but even if you
choose a 4 digit number it suggests you choose a larger one next time,
and indeed, it will work with a 5 digit number as well.
As I said, this type of puzzle isn't completely unfamiliar. I started
off looking at the algebra involved. A four digit number can be
represented as:
1000w + 100x + 10y + z
Where w, x, y, and z are any digit from 0 to 9 (except w, which can't
be a 0 or it would be a three digit number)
If you jumble it, the second number will look something like this:
1000x + 100z + 10y + w
When you subtract the smaller of these two numbers from the other, you
get:
999w - 900x - 99z or possibly -(999w - 900x - 99z), whichever is
positive. Note that the y's cancelled out because y didn't change
position
In fact, the result will be the sum of one value (or the negative of
the value) from each of these four lines:
9w, 99w, 999w, 90w, 990w, 900w, or 0w if it doesn't change position
9x, 99x, 999x, 90x, 990x, 900x, or 0x if it doesn't change position
9y, 99y, 999y, 90y, 990y, 900y, or 0y if it doesn't change position
9z, 99z, 999z, 90z, 990z, 900z, or 0z if it doesn't change position
The reason for the above is fairly straightforward. There are only so
many different pairs of positions the number can have: it can be in the
thousands place and the ones place, which will result in either 999 or
-999 times that number in the resulting difference.
The thing to note about the above is that each of the coefficients is
divisible by 9. "Aha!" the math puzzle solver cries, "Now we're getting
somewhere!"
The reason for the outburst is that all numbers divisible by 9 have
digits that add up to 9 or a multiple of 9. So for example:
27 2 + 7 = 9
99 9 + 9 = 18
171 1 + 7 + 1 = 9
etc.
This works for any multiple of 9. The proof is either beyond the scope
of this writing, or left as an exercise for the reader, take your pick
;-)
Now, the truth so often exploited in puzzles like this is that if you
have a number that deviates from a multiple of 9 in some way, and you
know what that way is, you can tell the original number because you
know the digits must add up to 9.
Here it's fairly straightforward. Once you've calculated the
difference, you are supposed to pick one digit of the result and tell
the program the others. The program knows the digits must add up to 9,
so if you tell it the other digits are (in no particular order -- order
doesn't matter because we're summing):
3, 2, 8
Then the program adds those numbers,
gets 13
and therefore knows that the missing digit must be 5 in order to add up
to 18 and make it a multiple of 9.
The program asks you not to choose 0, which is a little funny. If the
digits add up to a multiple of 9 it should know you chose 0. But I
suppose you could have entered all the digits, and it would have no way
to know whether you had chosen 0 or simply not chosen anything at all.
Bringing this back to Revolution, here's code I wrote to generate some
numbers and the resulting sums. Note how the third number in each line
displayed is divisible by 9. To be thorough you'd have to rewrite this
to check every possible combination of numbers and scrambling, but I'm
done now ;-) The great thing about Revolution is how easy it is to bang
out something like this. The initial code took maybe five minutes to
write.
on mouseUp
repeat 100
put random(9000) + 999 into tNumber
put empty into tScrambledNumber
put tNumber into temp
repeat 3
repeat with i = 4 down to 1
put random(i) into tWhichDigit
put char tWhichDigit of temp after tScrambledNumber
delete char tWhichDigit of temp
end repeat
if tScrambledNumber is not tNumber then exit repeat
end repeat
put abs(tNumber - tScrambledNumber) into tDifference
put 0 into tSum
repeat for each char c in tDifference
add c to tSum
end repeat
put tNumber && tScrambledNumber && tSum & cr after fld 1
end repeat
end mouseUp
regards,
Geoff Canyon
gcanyon at inspiredlogic.com
More information about the use-livecode
mailing list