A Diophantine equation is an indeterminate polynomial equation that allows the variables to be integers only.
Perl5.10 regexes provide extensions that make much easier to begin to deal with nonlinear Diophantine equations.
The following story sets a mathematical challenge that leads to a system of two Diophantine equations:
Two MIT math grads bump into each other while shopping at Fry’s. They
haven't seen each other in over 20 years. First grad to the second: "How have you been?"
The problem is to know the ages of the three daughters.
Second: "Great! I got married and I have three daughters now."
First: "Really? How old are they?"
Second: "Well, the product of their ages is 72, and the sum of their ages is the same as the number on that building over there..."
First: "Right, ok... Oh wait... Hmm, I still don't know."
Second: "Oh sorry, the oldest one just started to play the piano."
First: "Wonderful! My oldest is the same age!"
The following code uses the matching ('1'x72) =~ /^((1+)\2+)(\1+)$(?{ f($1, $2, $3) })(*FAIL)/ to produce all the solutions of the Diophantine equation x*y*z = 72:
When executed, this program produces the set of triples whose product is 72. The second column contains the potential number on the mentioned building:use v5.10; use strict; use List::Util qw{sum}; my $product = shift || 72; local our %u; sub f { my @a = @_; @a = sort { $b <=> $a } (length($a[1]), length($a[0])/length($a[1]), + $product/length($a[0]) ); local $" = ", "; say "(@a)\t ".sum(@a) unless exists($u{"@a"}); $u{"@a"} = undef; } say "SOL\t\tNUMBER"; my @a = ('1'x$product) =~ /^((1+)\2+)(\1+)$ (?{ f($1, $2, $3) }) (*FAIL) /x;
Only if the number on that building is 14 there is more than one solution. All other cases produce a single solution. But the first math grad, in spite of having access to the two equations$ ./oldestplayspiano.pl SOL NUMBER (18, 2, 2) 22 (12, 3, 2) 17 (9, 4, 2) 15 (8, 3, 3) 14 (6, 6, 2) 14 (6, 4, 3) 13 (36, 2, 1) 39 (24, 3, 1) 28 (18, 4, 1) 23 (12, 6, 1) 19 (9, 8, 1) 18
still says:x*y*z = 72 x+y+z = number on that building over there...
"Right, ok... Oh wait... Hmm, I still don't know."Thus the number of the building was 14 and the solution is one of:
... but we know the "oldest one just started to play the piano".(8, 3, 3) (6, 6, 2)
Do you know of other "freak" examples of using regexes to solve combinatorial problems?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: The Oldest Plays the Piano
by blokhead (Monsignor) on Sep 22, 2009 at 03:31 UTC | |
by ambrus (Abbot) on Sep 22, 2009 at 12:15 UTC | |
by ambrus (Abbot) on Dec 22, 2009 at 12:09 UTC | |
Re: The Oldest Plays the Piano
by CountZero (Bishop) on Sep 21, 2009 at 22:33 UTC | |
by didier (Vicar) on Sep 24, 2009 at 19:42 UTC | |
by CountZero (Bishop) on Sep 24, 2009 at 20:58 UTC | |
Re: The Oldest Plays the Piano
by deMize (Monk) on Sep 22, 2009 at 20:10 UTC | |
by casiano (Pilgrim) on Sep 23, 2009 at 07:23 UTC | |
by deMize (Monk) on Sep 23, 2009 at 13:06 UTC | |
by markuhs (Scribe) on Oct 16, 2009 at 10:46 UTC | |
Re: The Oldest Plays the Piano
by spx2 (Deacon) on Sep 24, 2009 at 11:01 UTC | |
by grizzley (Chaplain) on Oct 16, 2009 at 12:14 UTC | |
by Anonymous Monk on Oct 13, 2009 at 19:39 UTC |