P is for Practical PerlMonks

### Diophantine Equation Solver

by japhy (Canon)
 on Apr 11, 2000 at 16:25 UTC Need Help??

```   1: # working on making it allow negative numbers via some
2: # crazy form of look-behind and look-ahead
3:
4: # NOTE: just because you CAN use a regex to do this
5: # doesn't mean you should!
6:
7: use Carp;
8:
9: if (%results = solve_dio('5x + 7y + 4z = 50')) {
10:   print map "\$_ => \$results{\$_}\n", keys %results;
11: }
12:
13: if (solve_dio(x => 3, y => 2, z => 5, 102)) {
14:   print "solution found!\n";
15: }
16:
17:
18: # solve_dio('3x + 2y + 5z = 102');
19: # solve_dio(x => 3, y => 2, z => 5, 102);
20:
21: sub solve_dio {
22:   my (\$sum,%args,\$regex,@values);
23:
24:   if (@_ > 1) {
25:     \$sum = pop;
26:     %args = @_;
27:   }
28:   elsif (@_ == 1) {
29:     (\$sum,%args) = parseEQN(shift);
30:   }
31:   else {
32:     croak << "USAGE";
33: not enough args to solve_dio()
34:   solve_dio(EQN)
35:   solve_dio(VAR => COEFF, [VAR => COEFF, ] SUM)
36: USAGE
37:   }
38:
39:   \$regex = join " ", map "((?:@{[ 0 x \$args{\$_} ]})+)", keys %args;
40:
41:   if (@values = (0 x \$sum) =~ /^ \$regex \$/x) {
42:     return 1 if !wantarray;
43:     for (@values) {
44:       my (\$var) = each %args;
45:       \$args{\$var} = length() / \$args{\$var};
46:     }
47:     return %args;
48:   }
49: }
50:
51: sub parseEQN {
52:   (my \$EQN = shift) =~ tr/A-Za-z0-9 //cd;
53:   my (@parts,\$sum,%args);
54:
55:   @parts = split ' ', \$EQN;
56:   \$sum = pop @parts;
57:   %args = map reverse(/(\d+)([a-z])/), @parts;
58:
59:   return (\$sum, %args);
60: }```

Replies are listed 'Best First'.
RE: Diophantine Equation Solver
by kryten (Scribe) on Apr 11, 2000 at 18:39 UTC
For all, like me, wondering what a Diophantine equation actually is:

Diophantine equation
(from: britannica.com who seem to hand out far too many cookies.)
```[An] equation involving only addition, multiplication, or taking power
+s in which all the constants are
natural numbers or their negatives and the only solutions of interest
+are natural numbers or their
negatives. Named in honour of the 3rd-century Greek mathematician Diop
+hantus of Alexandria,
these equations were not systematically solved until the 7th century b
+y the Hindus.
Maybe a maths type person can elaborate.

Create A New User
Node Status?
node history
Node Type: perlcraft [id://7301]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (9)
As of 2019-10-23 15:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In 2019 the site I miss most is:

Results (58 votes). Check out past polls.

Notices?