Sum to 100: Difference between revisions

3,015 bytes added ,  2 years ago
Added 11l
m (→‎{{header|Phix}}: added personal tag)
(Added 11l)
Line 32:
<br>(which, &nbsp; of course, &nbsp; isn't the lowest positive sum that can't be expressed).
<br><br>
 
=={{header|11l}}==
{{trans|Kotlin}}
 
<lang 11l>-V
NUMBER_OF_DIGITS = 9
THREE_POW_4 = 3 * 3 * 3 * 3
NUMBER_OF_EXPRESSIONS = 2 * THREE_POW_4 * THREE_POW_4
 
T.enum Op
ADD
SUB
JOIN
 
T Expression
code = [Op.ADD] * :NUMBER_OF_DIGITS
 
F inc()
L(i) 0 .< .code.len
.code[i] = Op((Int(.code[i]) + 1) % 3)
I .code[i] != ADD
L.break
 
F toInt()
V value = 0
V number = 0
V sign = 1
L(digit) 1..9
V c = .code[:NUMBER_OF_DIGITS - digit]
I c == ADD {value += sign * number; number = digit; sign = 1}
E I c == SUB {value += sign * number; number = digit; sign = -1}
E {number = 10 * number + digit}
R value + sign * number
 
F String()
V s = ‘’
L(digit) 1 .. :NUMBER_OF_DIGITS
V c = .code[:NUMBER_OF_DIGITS - digit]
I c == ADD
I digit > 1
s ‘’= ‘ + ’
E I c == SUB
s ‘’= ‘ - ’
s ‘’= String(digit)
R s.ltrim(‘ ’)
 
F printe(givenSum)
V expression = Expression()
L 0 .< :NUMBER_OF_EXPRESSIONS
I expression.toInt() == givenSum
print(‘#9’.format(givenSum)‘ = ’expression)
expression.inc()
 
T Stat
DefaultDict[Int, Int] countSum
DefaultDict[Int, Set[Int]] sumCount
F ()
V expression = Expression()
L 0 .< :NUMBER_OF_EXPRESSIONS
V sum = expression.toInt()
.countSum[sum]++
expression.inc()
L(k, v) .countSum
.sumCount[v].add(k)
 
print("100 has the following solutions:\n")
printe(100)
 
V stat = Stat()
V maxCount = max(stat.sumCount.keys())
V maxSum = max(stat.sumCount[maxCount])
print("\n#. has the maximum number of solutions, namely #.".format(maxSum, maxCount))
 
V value = 0
L value C stat.countSum
value++
print("\n#. is the lowest positive number with no solutions".format(value))
 
print("\nThe ten highest numbers that do have solutions are:\n")
L(i) sorted(stat.countSum.keys(), reverse' 1B)[0.<10]
printe(i)</lang>
 
{{out}}
<pre>
100 has the following solutions:
 
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
 
9 has the maximum number of solutions, namely 46
 
211 is the lowest positive number with no solutions
 
The ten highest numbers that do have solutions 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>
 
=={{header|Ada}}==
1,481

edits