Solving a Linear Diophantine Equation(see description for examples)

前端 未结 8 886
忘掉有多难
忘掉有多难 2020-12-16 20:27

Let me start off by clarifying that(before you guys dismiss me), this is not a homework problem and I\'m not a university student. :)

EDIT Thanks to

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 21:08

    This is a solution in Perl. rather a hack by using Regex.

    Following this blog post to solve algebraic equations using regex.

    we can use the following script for 3x + 2y + 5z = 40

    #!/usr/bin/perl
    $_ = 'o' x 40;
    $a = 'o' x 3;
    $b = 'o' x 2;
    $c = 'o' x 5;
    $_ =~ /^((?:$a)+)((?:$b)+)((?:$c)+)$/;
    print "x = ", length($1)/length($a), "\n";
    print "y = ", length($2)/length($b), "\n";
    print "z = ", length($3)/length($c), "\n";
    

    output: x=11, y = 1, z = 1

    the famous Oldest plays the piano puzzle ends up as a 3 variable equation

    This method applies for a condition that the variables are actually positive and the constant is positive.

提交回复
热议问题