Solving coin problems: Difference between revisions

m
m (→‎{{header|Phix}}: added syntax colouring, made p2js compatible)
m (→‎{{header|Wren}}: Minor tidy)
 
(One intermediate revision by one other user not shown)
Line 30:
=={{header|Go}}==
This relatively simple program can only solve problems with 2 types of coins (or other objects) or 3 types of coins (but not other objects) without the need for an equation solver. However, it is able to solve all 28 problems of these types which were originally listed in the Perl entry before it was restricted to the subset of those problems (24) involving coins and bills.
<langsyntaxhighlight lang="go">package main
 
import (
Line 428:
printAnswers(p, kinds)
}
}</langsyntaxhighlight>
 
{{out}}
Line 519:
=={{header|Julia}}==
Uses the JuMP optimization library as a solver. The examples are from the Go code, and a few regex are from the Perl code.
<langsyntaxhighlight lang="julia">using JuMP, GLPK
 
const nums = Dict(
Line 821:
coinproblemsolver(DATA)
 
</langsyntaxhighlight>{{out}}
<pre>
If a person has three times as many quarters as dimes and the total amount of money is $5.95,
Line 949:
=={{header|Perl}}==
Coin-type 'word problems' are analyzed into their constituent algebraic relationships, in a format suitable for processing by MAXIMA, a free computer algebra system. NB: MAXIMA <b>must</b> be locally installed for this task to function.
<langsyntaxhighlight lang="perl">use strict;
use warnings;
 
Line 1,133:
Ben has $45.25 in quarters and dimes. If he has 29 less quarters than dimes, how many of each type of coin does he have?
 
A person has 12 coins consisting of dimes and pennies. If the total amount of money is $0.30, how many of each coin are there?</langsyntaxhighlight>
{{out}}
<pre>problem: person 3 times as many quarter as dime and total amount money $ 5.95 , find number quarter and dime .
Line 1,214:
though all examples below are for 2 and 3 only. A couple (14 and 17) also sail perilously close to getting a divide by zero.
This task was quite a bit of fun, once I got stuck in.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Solving_coin_problems.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 1,673:
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #000000;">vused</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span> <span style="color: #0000FF;">?{</span><span style="color: #008000;">"unused vocab"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">vocab</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
{{out}}
You just gotta love this Pidgin English! The problem numbering system used below is mine alone.<br>
Line 1,741:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./dynamic" for Struct
import "./pattern" for Pattern
import "./str" for Str
import "./sort" for Sort
import "./fmt" for Fmt
 
var Kind = Struct.create("Kind", ["name", "value", "number"])
Line 2,120:
kinds[1].number = tn - kinds[0].number
printAnswers.call(p, kinds)
}</langsyntaxhighlight>
 
{{out}}
9,479

edits