Sum to 100: Difference between revisions

Added AutoHotkey
m (Added Delphi reference to Pascal code)
(Added AutoHotkey)
Line 781:
 
=={{header|AutoHotkey}}==
<lang AutoHotkey>global Matchesoutput:=[]""
{{incomplete|AutoHotkey| <br><br> The output is incomplete, please address the 2<sup>nd</sup> and 3<sup>rd</sup> task requirements. <br><br>}}
for k, v in (sum2num(100))
 
res output .= eqk "`n"
Inspired by https://autohotkey.com/board/topic/149914-five-challenges-to-do-in-an-hour/
MsgBox, 262144, , % output
<lang AutoHotkey>global Matches:=[]
}
AllPossibilities100()
mx := []
for eq, val in matches
loop 123456789{
res .= eq "`n"
x := sum2num(A_Index)
MsgBox % res
mx[x.Count()] := mx[x.Count()] ? mx[x.Count()] ", " A_Index : A_Index
}
MsgBox, 262144, , % mx[mx.MaxIndex()] " has " mx.MaxIndex() " solutions"
loop {
if !sum2num(A_Index).Count(){
MsgBox, 262144, , % "Lowest positive sum that can't be expressed is " A_Index
break
}
}
return
 
sum2num(num){
AllPossibilities100(n:=0, S:="") {
output := []
if (n = 0) ; First call
loop % 6561
AllPossibilities100(n+1, n) ; Recurse
else if (n < 10){
oper := SubStr("00000000" ConvertBase(10, 3, A_Index-1), -7)
AllPossibilities100(n+1, S ",-" n) ; Recurse. Concatenate S, ",-" and n
oper := StrReplace(oper, 0, "+")
AllPossibilities100(n+1, S ",+" n) ; Recurse. Concatenate S, ",+" and n
oper := StrReplace(oper, 1, "-")
AllPossibilities100(n+1, S n) ; Recurse. Concatenate S and n
oper := StrReplace(oper, 2, ".")
} else { ; 10th level recursion
str := ""
Loop, Parse, S, CSV ; Total the values of S and check if equal to 100
loop 9
{
str .= A_Index . SubStr(oper, A_Index, 1)
SubS := SubStr(A_LoopField, 2) ; The number portion of A_LoopField
str := StrReplace(str, ".")
if (A_Index = 1)
loop 2
Total := A_LoopField
{
else if (SubStr(A_LoopField, 1, 1) = "+") ; If the first character is + add
val := 0
Total += SubS
for i, v in StrSplit(str, "+")
else ; else subtract
for j, m in StrSplit(v, "-")
Total -= SubS
val += A_Index=1 ? m : 0-m
}
if (Totalval = 100num)
output[str] := true
matches[LTrim(LTrim(StrReplace(S, ","), "0"),"+")] := true ; remove leading 0's, +'s and all commas
str := "-" str
}
}
}
Sort, output
return output
}
; https://www.autohotkey.com/boards/viewtopic.php?p=21143&sid=02b9c92ea98737f1db6067b80a2a59cd#p21143
ConvertBase(InputBase, OutputBase, nptr){
static u := A_IsUnicode ? "_wcstoui64" : "_strtoui64"
static v := A_IsUnicode ? "_i64tow" : "_i64toa"
VarSetCapacity(s, 66, 0)
value := DllCall("msvcrt.dll\" u, "Str", nptr, "UInt", 0, "UInt", InputBase, "CDECL Int64")
DllCall("msvcrt.dll\" v, "Int64", value, "Str", s, "UInt", OutputBase, "CDECL")
return s
}</lang>
{{out}}
Outputs:<pre>-1+2-3+4+5+6+78+9
<pre>
---------------------------
Outputs:<pre>-1+2-3+4+5+6+78+9
1+2+3-4+5+6+78+9
1+2+34-5+67-8+9
Line 824 ⟶ 851:
123+45-67+8-9
123-4-5-6-7+8-9
123-45-67+89</pre>
---------------------------
9 has 46 solutions
---------------------------
Lowest positive sum that can't be expressed is 211
---------------------------
</pre>
 
=={{header|AWK}}==
299

edits