This is my latest effort. Replace $ with < and % with> globally.
Quote:
$DOCTYPE html%
$head%
$title%Gazendas$/title%
$script language="JavaScript"%
function sample() {
document.bingo.inputArea.value="532.72\n298.01\n97.24\n32.53 4\n174.56\n70.78\n99.28\n193.82"
calculate();
}
function mySort(a,b) {return a.amt - b.amt} ;
function Nformat(parm) {
var aparm=(Math.round(parm * 100)) / 100;
var rstring = aparm.toString()
var i = rstring.indexOf(".")
if ( i < 0) {
rstring = rstring + ".00"
i = rstring.indexOf(".")
}
if (rstring.length - i == 2)
rstring = rstring + "0"
if (parm < 10.00)
rstring = " " + rstring;
return rstring
}
function resultFunction(amt,string) {
this.amt = Math.abs(amt); // Holds the absolute value of the difference (for sorting)
this.sign=(amt < 0)? "-" : " "; // sign of difference
this.text = string; // just built string
}
// One is added to each multiplier (number or occurances). If an item can occur 2 times; ZERO, one, and two
// must be considered.
function candidateFunction(amt,mult){
this.amt = amt;
this.mult = mult + 1;
this.try=0; // Will be manipulated later, but must start at zero.
}
function calculate() {
var i, j, carry, lines, target, asum, mult, amt, maxTry=1, result = new(Array), candidates=new(Array), results= new(Array);
var buildlines=" Diff List of items Considering ", oneline="";
lines = document.bingo.inputArea.value.split("\n");
target=parseFloat(lines[0]);
//alert(target);
for (i=1; i < lines.length; i++) {
result = lines[i].match(/ *(\d+\.?\d*)\D?/)
if (result == null)
break;
amt = parseFloat(result[1]);
result = lines[i].match(/ *[^ ]+ +(\d+)/)
if (result == null)
mult=1;
else
mult=parseInt(result[1]);
candidates[i-1] = new candidateFunction(amt,mult); } //Function will add 1 to multiplier
// alert("Length of candidates: " + candidates.length);
for (i=0; i < candidates.length; i++)
maxTry *= candidates[i].mult;
//alert("Max tries: " + maxTry);
for (i=2; i< maxTry;i++) { // Omitting nothing and everything
j=0;
do {
candidates[j].try++; // Add 1
if (candidates[j].try == candidates[j].mult) { // Need to carry
carry=1;candidates[j].try=0;j++; } // Zero this column, move to next
else
carry=0; } while (carry); // If no carry, we are done.
asum=0,oneline=""; // Prep to run this candidate row
for (j=0; j < candidates.length; j++) {
if (candidates[j].try) { // If zero, ignore
var oneMult = candidates[j].try * candidates[j].amt;
if (asum) // If something on line already
oneline+= " + "; // append a + sign
asum += oneMult;
oneline+=Nformat(candidates[j].amt); // Will append * and # times if over 1.
oneline+= (candidates[j].try > 1)? " * " + candidates[j].try : ""; } }
results[i-2] = new resultFunction(asum - target, oneline) } // Finished this set, add to results
buildlines+= (maxTry - 2) + " arrangements\n"; //Less nothing and everything tries
results.sort(mySort); // Moves best answer to the top
j=(results.length < 50)? results.length : 50; //Show top 50 rows with smallest Diff
for (i=0; i < j; i++)
buildlines+= Nformat(results[i].amt) + results[i].sign +" " + results[i].text + "\n";
document.bingo.outputArea.value = buildlines;
}
$/script%
$/head%
$body onLoad="document.bingo.outputArea.value=''"%
$h1%$center%Gazendas$/center%$/h1%
$form name="bingo"%
$table%
$tr%$th colspan=3%Enter target sum on first row. On other rows give candidate amounts and (optional) a space and multiplier (example 20.00 4 for four twenty dollar checks).$/th%$/tr%
$tr%$td%$textarea name="inputArea" rows="20" cols="10"%$/textarea%$/td%$td colspan=2%$textarea name="outputArea" rows="20" cols="100" %$/textarea%$/td%$/tr%
$tr%$td%$input type="button" value="Submit" onClick="calculate()"%$/td%$td%$input type="button" value="Sample Data" onClick="sample()"%
$input type="button" value="Reset" onClick='document.bingo.inputArea.value="";document.bingo.outputArea.value="";'%$/td%$/tr%
$/table%
The name comes from a routine on the program Hee-Haw ("four gazenda 20 five times"), but this page shows which items go into a specified sum. For example, you have written checks totaling
about 800.00 but your bank balence only goes down by 310.25. To determine which checks have cleared, put 310.25 on the first line and the amounts of the outstanding checks on the next lines.
If you have more than one check for the same amount, this page needs the amount, a space, and count of that size check. This page will run all combinations of cashed and uncashed
checks on your list and give you the closest results first (Diff equal or near zero).$br%$br%
User assumes all risks such as more than one correct answer, other fees, mis-entered numbers, and/or extra/missing numbers.$br%$br%
Sample assumes checks totaling 532.72 have been cashed from six unique amounts and four checks of 32.53 each. We don't consider none or all.$br%$br%
If a Difference is negative, the sum of the items is too small; if more than zero, too much.
$script%
if (Date.parse(document.lastModified) != 0)
document.write('$p%$hr%$small%$i%Last modified: '
+ document.lastModified
+ '$/i%$/small%$br%');
$/script%
$/body%
$/html%