Jump to content

Solve equations with substitution method: Difference between revisions

(Added Algol 68)
Line 532:
 
</pre>
 
=={{header|jq}}==
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
The solution presented here handles all the edge cases.
 
With trivial modifications, the following will also work with jaq, the Rust implementation of jq.
<syntaxhighlight lang="jq">
# The equation ax + by = c is represented by the array [a, b, c]
def solve( $e1; $e2 ):
$e1 as [$a1, $b1, $c1]
| $e2 as [$a2, $b2, $c2]
| ($b2 * $a1 - $b1 * $a2 ) as $d
| if $d == 0 then "there is no unique solution as the discriminant is 0" | error
else
{ x : (($b2 * $c1 - $b1 * $c2 ) / $d) }
| if $b1 != 0
then .y = ( $a1 * .x - $c1 ) / -$b1
else .y = ( $a2 * .x - $c2 ) / -$b2
end
end;
 
solve( [3,1,-1]; [2,-3,-19] )
</syntaxhighlight>
{{output}}
<pre>
{
"x": -2,
"y": 5
}
</pre>
 
 
=={{header|Julia}}==
2,471

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.