Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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?


In reply to The Oldest Plays the Piano by casiano

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-03-29 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found