December 19, 2006

Innumerate Insight

Filed under: Puzzles — mark @ 9:30 pm

… or following up to the last post, maybe this is ‘Et Three Brute’.

My niece came home with her own fifth grade math challenge: For what four-digit number EFGH can it be said EFGH x 4 = HGFE

My sister-in-law was annoyed by this, because by the time I saw it, the problem had stumped my niece and every adult in the room. She observed that a puzzle like this, sprung on a kid unawares too early can cause them to lose confidence in their own mathematical creativity. I think she’s right. She also said that she was going to tell her daughter to just say all the digits were zero and skip the problem.

She’s smarter than she lets on … 0000 is one of the two correct answers! (The degenerate answer, snooty math majors would say, suggesting that the format of the problem indicates E, F, G, and H are different numbers. But the problem doesn’t say that.)

I’m terrible at these, but gave it a shot. All multiples of 4 end with either 0, 2, 4, 6 or 8. Surveying that bunch, it seemed likely that we were looking at 2 thousand something times 4 would be 8 thousand something, so we’d get:

2FG8 x 4 = 8GF2

I figured it out from there through trial and error, but should have asked Dad. He pointed out that F had to be less than 3 (2300 x 4 would be 9200 … too big). That leaves 2, 1, and 0, and we are already using 2, so it’s 1 or 0. “And”, he says, “it can’t be zero, because any number ending ‘02′ divided by 4 would have a remainder of 1/2." Oh yeah. That leaves 1.

So we have 21G8 x 4 = 8G12.

Using simple algebra 4 * (2108 + 10 * G) = 8012 + 100 * G, so

8432 + 40 * G = 8012 + 100 * G

420 = 60 * G, or G = 7

2178 * 4 = 8712

… and for anyone still paying attention, note that we left fifth grade math quite some time ago!

In 60 seconds you can write a Perl script:

#!/usr/bin/perl -w
use strict;

my $i;
my ($s, $t);

for ($i = 0;$i < 10000;$i++) {
    $s = sprintf("%04d",$i);
    $t = reverse($s);

    if (($s * 4) == $t) {
        print "$s * 4 = $t\n";
    }
}

… that in a fraction of a second tells you:

0000 * 4 = 0000
2178 * 4 = 8712

December 12, 2006

Et tu, Brute?

Filed under: Puzzles — mark @ 9:07 pm

Jake came home from fourth-grade math today with a certified Math Challenge: using the integers 1 to 9 only once each, find how many true sets of 3-digit numbers x, y, and z will satisfy the equation x + y = z.

The example given was 237 + 654 = 891. And we are told there are over 300 solutions.

God knows there must be an elegant mathematical approach to this, but man, there is a Science Quiz breathing down our neck for Friday, there are writing exercises to complete, and that Star Wars book isn’t going to read itself.

In short, it’s a perfect time to employ our friend, Brute Force.

After installing the perl module List-Permutor, we dash off the following script:

#!/usr/bin/perl -w
use List::Permutor;

my $perm;
my ($a, $b, $c);
my @set;
my $ctr = 0;

$perm = new List::Permutor qw/ 1 2 3 4 5 6 7 8 9 /;

while (@set = $perm->next) {
    $a = $set[0] . $set[1] . $set[2];
    $b = $set[3] . $set[4] . $set[5];
    $c = $set[6] . $set[7] . $set[8];

    if (($a + $b) == $c) {
        $ctr++;
        printf "%-3d: ", $ctr;
        print "$a + $b = $c\n";
    }
}

That’s right … generate every one of the 9! = 9*8*7*6*5*4*3*2*1 = 362,880 permutations of the characters 123456789, carve off the first three digits of each permutation to be x, the second three to be y, the third 3 to be z, and see if x + y = z. If it does, count the solution and print. There are 336 solutions (listed here).

Of course, while I was screwing around with this, Jake found two on his own … against staggering odds. That kid needs to be buying more scratch tickets.

So we have to go with his answer … besides, I don’t think they cover Perl until next term, so this would be difficult to explain.