How to resolve the algorithm Sum to 100 step by step in the zkl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sum to 100 step by step in the zkl programming language
Table of Contents
Problem Statement
Find solutions to the sum to one hundred puzzle.
Add (insert) the mathematical operators + or - (plus or minus) before any of the digits in the decimal numeric string 123456789 such that the resulting mathematical expression adds up to a particular sum (in this iconic case, 100).
Example:
Show all output here.
‡ (where infinity would be a relatively small 123,456,789)
An example of a sum that can't be expressed (within the rules of this task) is: 5074 (which, of course, isn't the lowest positive sum that can't be expressed).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sum to 100 step by step in the zkl programming language
Source code in the zkl programming language
var all = // ( (1,12,123...-1,-12,...), (2,23,...) ...)
(9).pump(List,fcn(n){ split("123456789"[n,*]) }) // 45
.apply(fcn(ns){ ns.extend(ns.copy().apply('*(-1))) }); // 90
fcn calcAllSums{ // calculate all 6572 sums (1715 unique)
fcn(n,sum,soFar,r){
if(n==9) return();
foreach b in (all[n]){
if(sum+b>=0 and b.abs()%10==9) r.appendV(sum+b,"%s%+d".fmt(soFar,b));
self.fcn(b.abs()%10,sum + b,"%s%+d".fmt(soFar,b),r);
}
}(0,0,"",r:=Dictionary());
r
}
// "123" --> (1,12,123)
fcn split(nstr){ (1).pump(nstr.len(),List,nstr.get.fp(0),"toInt") }
fcn showSums(allSums,N=100,printSolutions=2){
slns:=allSums.find(N,T);
if(printSolutions) println("%d solutions for N=%d".fmt(slns.len(),N));
if(printSolutions==2) println(slns.concat("\n"));
println();
}
allSums:=calcAllSums();
showSums(allSums);
showSums(allSums,0,1);
println("Smallest postive integer with no solution: ",
[1..].filter1('wrap(n){ Void==allSums.find(n) }));
println("5 commonest sums (sum, number of ways to calculate to it):");
ms:=allSums.values.apply("len").sort()[-5,*]; // 5 mostest sums
allSums.pump(List, // get those pairs
'wrap([(k,v)]){ v=v.len(); ms.holds(v) and T(k.toInt(),v) or Void.Skip })
.sort(fcn(kv1,kv2){ kv1[1]>kv2[1] }) // and sort
.println();
You may also check:How to resolve the algorithm Rhonda numbers step by step in the Perl programming language
You may also check:How to resolve the algorithm Display a linear combination step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Vampire number step by step in the Elixir programming language
You may also check:How to resolve the algorithm Remove lines from a file step by step in the C++ programming language
You may also check:How to resolve the algorithm Magic squares of doubly even order step by step in the Befunge programming language