Sum to 100: Difference between revisions

1,860 bytes added ,  2 months ago
→‎{{header|UNIX Shell}}: Add implementation.
m (→‎{{header|ALGOL 68}}: Avoid more line wrap)
(→‎{{header|UNIX Shell}}: Add implementation.)
Line 6,058:
</pre>
 
=={{header|UNIX Shell}}==
{{works with|Korn Shell}}
{{works with|Bourne Again SHell}}
{{works with|Z Shell}}
 
<syntaxhighlight lang=bash>sumto100() {
typeset max_count=0 i n sum max_values
typeset -A histogram
printf 'Strings that evaluate to 100:\n'
for n in {,-}1{,+,-}2{,+,-}3{,+,-}4{,+,-}5{,+,-}6{,+,-}7{,+,-}8{,+,-}9
do
(( sum = n ))
if (( sum == 100 )); then
printf '\t%s\n' "$n"
fi
histogram[$sum]=$(( ${histogram[$sum]:-0}+1 ))
if (( histogram[$sum] > max_count )); then
(( max_count = histogram[$sum] ))
max_values=( $sum )
elif (( histogram[$sum] == max_count )); then
max_values+=( $sum )
fi
done
echo "Most solutions for any number is $max_count, for ${max_values[@]}"
for (( i=1; i<123456789; ++i )); do
if (( !histogram[$i] )); then
echo "Lowest positive sum that can't be expressed: $i"
break
fi
done
printf 'Ten highest reachable numbers:\n';
if [[ -n $ZSH_VERSION ]]; then
printf '\t%s\n' "${(k@)histogram}"
else
printf '\t%s\n' "${!histogram[@]}"
fi | sort -nr | head -n 10
}
 
sumto100</syntaxhighlight>
{{Out}}
<pre>Strings that evaluate to 100:
123+45-67+8-9
123+4-5+67-89
123-45-67+89
123-4-5-6-7+8-9
12+3+4+5-6-7+89
12+3-4+5+67+8+9
12-3-4+5-6+7+89
1+23-4+56+7+8+9
1+23-4+5+6+78-9
1+2+34-5+67-8+9
1+2+3-4+5+6+78+9
-1+2-3+4+5+6+78+9
Most solutions for any number is 46, for 9 -9
Lowest positive sum that can't be expressed: 211
Ten highest reachable numbers:
123456789
23456790
23456788
12345687
12345669
3456801
3456792
3456790
3456788
3456786
</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.
1,480

edits