Sum to 100: Difference between revisions

8,442 bytes added ,  1 month ago
m
Minor edit to Rust code
m (→‎{{header|ALGOL 68}}: Avoid line wrap)
m (Minor edit to Rust code)
 
(6 intermediate revisions by 4 users not shown)
Line 521:
FI
OD;
print( ( whole( number with max, 0 ), " has the maximum number of solutions: ", whole( max solutions, 0 ), newline ) );
print( ( whole( max solutions, 0 ), newline ) );
# find the smallest positive number that has no solutions #
BOOL have solutions := TRUE;
Line 1,984 ⟶ 1,985:
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Sum_to_100#Pascal Pascal].
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
ADD = 0
SUB = 1
nexpr = 13122 - 1
len f[] nexpr + 1
arrbase f[] 0
#
func evaluate code .
power = 1
for k = 9 downto 1
numb += power * k
m = code mod 3
if m = ADD
value += numb
numb = 0
power = 1
elif m = SUB
value -= numb
numb = 0
power = 1
else
power *= 10
.
code = code div 3
.
return value
.
proc init . .
for i = 0 to nexpr
f[i] = evaluate i
.
.
call init
proc out code . .
a = 19683
b = 6561
for k = 1 to 9
h = code mod a div b
if h = ADD
if k > 1
s$ &= "+"
.
elif h = SUB
s$ &= "-"
.
a = b
b = b div 3
s$ &= strchar (48 + k)
.
print evaluate code & " = " & s$
.
print "Show all solutions that sum to 100\n"
for i = 0 to nexpr
if f[i] = 100
out i
.
.
print "\nShow the sum that has the maximum number of solutions\n"
for i = 0 to nexpr
test = f[i]
if test > 0
ntest = 0
for j = 0 to nexpr
if f[j] = test
ntest += 1
.
.
if ntest > nbest
best = test
nbest = ntest
.
.
.
print best & " has " & nbest & " solutions"
print "\nShow the lowest positive number that can't be expressed\n"
for i = 0 to 123456789
for j = 0 to nexpr
if i = f[j]
break 1
.
.
if j > nexpr
break 1
.
.
print i
print "\nShow the ten highest numbers that can be expressed\n"
limit = 123456789 + 1
for i = 1 to 10
best = 0
for j = 0 to nexpr
test = f[j]
if test < limit and test > best
best = test
.
.
for j = 0 to nexpr
if f[j] = best
out j
.
.
limit = best
.
</syntaxhighlight>
{{out}}
<pre>
Show all solutions that sum to 100
 
100 = 1+2+3-4+5+6+78+9
100 = 1+2+34-5+67-8+9
100 = 1+23-4+5+6+78-9
100 = 1+23-4+56+7+8+9
100 = 12+3+4+5-6-7+89
100 = 12+3-4+5+67+8+9
100 = 12-3-4+5-6+7+89
100 = 123+4-5+67-89
100 = 123+45-67+8-9
100 = 123-4-5-6-7+8-9
100 = 123-45-67+89
100 = -1+2-3+4+5+6+78+9
 
Show the sum that has the maximum number of solutions
 
9 has 46 solutions
 
Show the lowest positive number that can't be expressed
 
211
 
Show the ten highest numbers that can be expressed
 
123456789 = 123456789
23456790 = 1+23456789
23456788 = -1+23456789
12345687 = 12345678+9
12345669 = 12345678-9
3456801 = 12+3456789
3456792 = 1+2+3456789
3456790 = -1+2+3456789
3456788 = 1-2+3456789
3456786 = -1-2+3456789
</pre>
 
=={{header|Elixir}}==
Line 2,053 ⟶ 2,199:
3456788, 3456786]
</pre>
 
 
 
=={{header|F_Sharp|F#}}==
Line 5,881 ⟶ 6,025:
3456788: 1-2+3456789
3456786: -1-2+3456789
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">use std::collections::BTreeMap;
use std::fmt;
 
#[derive(Copy, Clone)]
enum Operator {
None,
Plus,
Minus,
}
 
#[derive(Clone)]
struct Expression {
ops: [Operator; 9],
}
 
impl Expression {
fn new() -> Expression {
Expression {
ops: [Operator::None; 9],
}
}
fn sum(&self) -> i32 {
let mut result: i32 = 0;
let mut n: i32 = 0;
let mut p: i32 = 1;
let mut i: usize = 9;
while i > 0 {
n += p * (i as i32);
i -= 1;
match self.ops[i] {
Operator::None => p *= 10,
Operator::Plus => {
p = 1;
result += n;
n = 0;
}
Operator::Minus => {
p = 1;
result -= n;
n = 0;
}
}
}
result += n;
result
}
fn next(&mut self) -> bool {
let mut i: usize = 9;
while i > 0 {
i -= 1;
match self.ops[i] {
Operator::None => {
self.ops[i] = if i == 0 {
Operator::Minus
} else {
Operator::Plus
};
return true;
}
Operator::Plus => {
self.ops[i] = Operator::Minus;
return true;
}
Operator::Minus => self.ops[i] = Operator::None,
}
}
false
}
}
 
impl fmt::Display for Expression {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for i in 0..9 {
match self.ops[i] {
Operator::None => {}
Operator::Plus => write!(f, "+")?,
Operator::Minus => write!(f, "-")?,
}
write!(f, "{}", i + 1)?;
}
Ok(())
}
}
 
fn main() {
let mut exp = Expression::new();
let mut sums: BTreeMap<i32, Vec<Expression>> = BTreeMap::new();
loop {
sums.entry(exp.sum()).or_insert(Vec::new()).push(exp.clone());
if !exp.next() {
break;
}
}
 
println!("Solutions that sum to 100:");
if let Some(expressions) = sums.get(&100) {
for e in expressions {
println!("100 = {}", e);
}
}
 
let mut max_sum = 0;
let mut max_count = 0;
for (sum, expressions) in &sums {
let count = expressions.len();
if count > max_count {
max_count = count;
max_sum = *sum;
}
}
println!(
"\nThe sum with the greatest number of solutions is {} ({}).",
max_sum, max_count
);
 
let mut n = 1;
while sums.contains_key(&n) {
n += 1;
}
println!(
"\nThe smallest positive number that cannot be expressed is {}.",
n
);
 
println!("\nThe ten highest numbers that can be expressed are:");
for (sum, expressions) in sums.iter().rev().take(10) {
println!("{} = {}", sum, expressions[0]);
}
}</syntaxhighlight>
 
{{out}}
<pre>
Solutions that sum to 100:
100 = 123+45-67+8-9
100 = 123+4-5+67-89
100 = 123-45-67+89
100 = 123-4-5-6-7+8-9
100 = 12+3+4+5-6-7+89
100 = 12+3-4+5+67+8+9
100 = 12-3-4+5-6+7+89
100 = 1+23-4+56+7+8+9
100 = 1+23-4+5+6+78-9
100 = 1+2+34-5+67-8+9
100 = 1+2+3-4+5+6+78+9
100 = -1+2-3+4+5+6+78+9
 
The sum with the greatest number of solutions is 9 (46).
 
The smallest positive number that cannot be expressed is 211.
 
The ten highest numbers that can be expressed are:
123456789 = 123456789
23456790 = 1+23456789
23456788 = -1+23456789
12345687 = 12345678+9
12345669 = 12345678-9
3456801 = 12+3456789
3456792 = 1+2+3456789
3456790 = -1+2+3456789
3456788 = 1-2+3456789
3456786 = -1-2+3456789
</pre>
 
Line 6,055 ⟶ 6,363:
highest 10:
123456789 23456790 23456788 12345687 12345669 3456801 3456792 3456790 3456788 3456786
</pre>
 
=={{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 max_values i
typeset -A histogram
printf 'Strings that evaluate to 100:\n'
for expr in {,-}1{,+,-}2{,+,-}3{,+,-}4{,+,-}5{,+,-}6{,+,-}7{,+,-}8{,+,-}9
do
(( sum = expr ))
if (( sum == 100 )); then
printf '\t%s\n' "$expr"
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
printf '\nMost solutions for any number is %d: ' "$max_count"
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
printf "Lowest positive sum that can't be expressed: %d\n\n" "$i"
break
fi
done
printf 'Ten highest reachable numbers:\n';
if [[ -n $ZSH_VERSION ]]; then
printf '\t%9d\n' "${(k@)histogram}"
else
printf '\t%9d\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: 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>
 
1,777

edits