Sum to 100: Difference between revisions
Added AutoHotkey
MaiconSoft (talk | contribs) m (Added Delphi reference to Pascal code) |
Alpha bravo (talk | contribs) (Added AutoHotkey) |
||
Line 781:
=={{header|AutoHotkey}}==
for k, v in (sum2num(100))
MsgBox, 262144, , % output
▲<lang AutoHotkey>global Matches:=[]
mx := []
loop 123456789{
▲ res .= eq "`n"
x := sum2num(A_Index)
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){
output := []
loop % 6561
oper := SubStr("00000000" ConvertBase(10, 3, A_Index-1), -7)
oper := StrReplace(oper, 0, "+")
oper := StrReplace(oper, 1, "-")
oper := StrReplace(oper, 2, ".")
str := ""
loop 9
str .= A_Index . SubStr(oper, A_Index, 1)
str := StrReplace(str, ".")
loop 2
{
val := 0
for i, v in StrSplit(str, "+")
for j, m in StrSplit(v, "-")
val += A_Index=1 ? m : 0-m
output[str] := true
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>
---------------------------
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
---------------------------
9 has 46 solutions
---------------------------
Lowest positive sum that can't be expressed is 211
---------------------------
</pre>
=={{header|AWK}}==
|