How to resolve the algorithm Sum to 100 step by step in the Nim programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Sum to 100 step by step in the Nim programming language
Table of Contents
Problem Statement
Find solutions to the sum to one hundred puzzle.
Add (insert) the mathematical operators + or - (plus or minus) before any of the digits in the decimal numeric string 123456789 such that the resulting mathematical expression adds up to a particular sum (in this iconic case, 100).
Example:
Show all output here.
‡ (where infinity would be a relatively small 123,456,789)
An example of a sum that can't be expressed (within the rules of this task) is: 5074 (which, of course, isn't the lowest positive sum that can't be expressed).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sum to 100 step by step in the Nim programming language
Source code in the nim programming language
import algorithm, parseutils, sequtils, strutils, tables
type Expression = string
proc buildExprs(start: Natural = 0): seq[Expression] =
let item = if start == 0: "" else: $start
if start == 9: return @[item]
for expr in buildExprs(start + 1):
result.add item & expr
result.add item & '-' & expr
if start != 0: result.add item & '+' & expr
proc evaluate(expr: Expression): int =
var idx = 0
var val: int
while idx < expr.len:
let n = expr.parseInt(val, idx)
inc idx, n
result += val
let exprs = buildExprs()
var counts: CountTable[int]
echo "The solutions for 100 are:"
for expr in exprs:
let sum = evaluate(expr)
if sum == 100: echo expr
if sum > 0: counts.inc(sum)
let (n, count) = counts.largest()
echo "\nThe maximum count of positive solutions is $1 for number $2.".format(count, n)
var s = 1
while true:
if s notin counts:
echo "\nThe smallest number than cannot be expressed is: $1.".format(s)
break
inc s
echo "\nThe ten highest numbers than can be expressed are:"
let numbers = sorted(toSeq(counts.keys), Descending)
echo numbers[0..9].join(", ")
import strutils
var
ligne: string = ""
sum: int
opera: array[0..9, int] = [0,0,1,1,1,1,1,1,1,1]
curseur: int = 9
boucle: bool
tot: array[1..123456789, int]
pG: int
plusGrandes: array[1..10, string]
let
ope: array[0..3, string] = ["-",""," +"," -"]
aAtteindre = 100
proc calcul(li: string): int =
var liS: seq[string]
liS = split(li," ")
for i in liS:
if i.len > 0: result += parseInt(i)
echo "Valeur à atteindre : ",aAtteindre
while opera[1]<2:
ligne.add(ope[opera[1]])
ligne.add("1")
for i in 2..9:
ligne.add(ope[opera[i]])
ligne.add($i)
sum = calcul(ligne)
if sum == aAtteindre:
stdout.write(ligne)
echo " = ",sum
if sum>0:
tot[sum] += 1
pG = 1
while pG<10:
if sum>calcul(plusGrandes[pG]):
for k in countdown(10,pG+1):
plusGrandes[k]=plusGrandes[k-1]
plusGrandes[pG]=ligne
pG = 11
pG += 1
ligne = ""
boucle = true
while boucle:
opera[curseur] += 1
if opera[curseur] == 4:
opera[curseur]=1
curseur -= 1
else:
curseur = 9
boucle = false
echo "Valeur atteinte ",tot[aAtteindre]," fois."
echo ""
var
min0: int = 0
max: int = 0
valmax: int = 0
for i in 1..123456789:
if tot[i]==0 and min0 == 0:
min0 = i
if tot[i]>max:
max = tot[i]
valmax = i
echo "Plus petite valeur ne pouvant pas être atteinte : ",min0
echo "Valeur atteinte le plus souvent : ",valmax,", atteinte ",max," fois."
echo ""
echo "Plus grandes valeurs pouvant être atteintes :"
for i in 1..10:
echo calcul(plusGrandes[i])," = ",plusGrandes[i]
You may also check:How to resolve the algorithm Mutual recursion step by step in the PHP programming language
You may also check:How to resolve the algorithm Read entire file step by step in the E programming language
You may also check:How to resolve the algorithm Associative array/Merging step by step in the Perl programming language
You may also check:How to resolve the algorithm Infinity step by step in the RPL programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the AWK programming language