Sum to 100: Difference between revisions
→{{header|UNIX Shell}}: Improve formatting of output, add version requirements.
(→{{header|UNIX Shell}}: Add implementation.) |
(→{{header|UNIX Shell}}: Improve formatting of output, add version requirements.) |
||
Line 6,059:
=={{header|UNIX Shell}}==
{{works with|Korn Shell|93+}}
{{works with|Bourne Again SHell|4.0+}}
{{works with|Z Shell|4.0+}}
<syntaxhighlight lang=bash>sumto100() {
typeset expr sum max_count=0
typeset -A histogram
printf 'Strings that evaluate to 100:\n'
for
do
(( sum =
if (( sum == 100 )); then
printf '\t%s\n' "$
fi
histogram[$sum]=$(( ${histogram[$sum]:-0}+1 ))
Line 6,081:
fi
done
if [[ -n $ZSH_VERSION ]]; then
printf '%s\n\n' "${(j:, :)max_values}"
else
printf '%s' "${max_values[0]}"
printf ', %s' "${max_values[@]:1}"
printf '\n\n'
fi
for (( i=1; i<123456789; ++i )); do
if (( !histogram[$i] )); then
break
fi
Line 6,090 ⟶ 6,098:
printf 'Ten highest reachable numbers:\n';
if [[ -n $ZSH_VERSION ]]; then
printf '\t%
else
printf '\t%
fi | sort -nr | head -n 10
}
sumto100
</syntaxhighlight>
{{Out}}
<pre>Strings that evaluate to 100:
Most solutions for any number is 46
Lowest positive sum that can't be expressed: 211
Ten highest reachable numbers:
</pre>
=={{header|Visual Basic .NET}}==
Of course, one could just code-convert the existing C# example, but I thought this could be written with some simpler constructs. The point of doing this is to make the code more compatible with other BASIC languages. Not every language has something similar to the <i>Enumerable Range</i> construct. I also found the <i>Dictionary</i> construct could be implemented with something more primitive.
|