[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[FWP] Diophantine Equation Solver (of much interest)



Robert Pike showed that a regular expression can be used to solve a simple
(positive coefficients only) diophantine equation:

  # 3x + 2y + 5z = 40
  /^((?:...)+)((?:..)+)((?:.....)+)$/ and do {
    print "x = ", length($1)/3, "\n";
    print "y = ", length($2)/2, "\n";
    print "z = ", length($3)/5, "\n";
  };

Well, a problem arises (arose?) with NEGATIVE coefficients, or a negative
sum for your diophantine equation.

Enter me.

Hi.

Through nastiness that can only be described as forcing two regular
expressions to be equal in result, I have come up with a nice chunk of
code.  Currently, it looks a bit nasty (I will clean it up in due
time).  It is more efficient than I'd thought it would be, considering it
uses regexes, and is full of Backtracking Delight.

It is meant to be a testament to the power of regexes, and the inanity of
the ends to which you can use them.  Please comment, gawk, ridicule, or
praise.

  http://www.pobox.com/~japhy/regexes/diophantine

I'll be commenting the code tomorrow (I'm leaving yapc now, and will not
be able to comment it until I'm home).

Thanks for your time.

-- 
Jeff "japhy" Pinyan     japhy@pobox.com     http://www.pobox.com/~japhy/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/
CPAN - #1 Perl Resource  (my id:  PINYAN)        http://search.cpan.org/


==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe