http://www.perlmonks.org?node_id=796576

According to the legend, it was abigail the first who started to solve Diophantine equations using regular expressions.

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?"
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 problem is to know the ages of the three daughters.

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:

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;
When executed, this program produces the set of triples whose product is 72. The second column contains the potential number on the mentioned building:
$ ./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
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
x*y*z = 72 x+y+z = number on that building over there...
still says:
"Right, ok... Oh wait... Hmm, I still don't know."
Thus the number of the building was 14 and the solution is one of:

(8, 3, 3) (6, 6, 2)
... but we know the "oldest one just started to play the piano".

Do you know of other "freak" examples of using regexes to solve combinatorial problems?