How to resolve the algorithm Sum to 100 step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Sum to 100 step by step in the Julia 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 Julia programming language

This Julia program generates and evaluates mathematical expressions using 9 variables and various operators to find patterns and solutions related to those expressions.

It starts by defining a function expr that takes a variable number of string arguments and concatenates them with the numbers 1 to 9, creating expressions like "1+2-3+4+5-6+7+8+9". The genexpr function generates a vector of such expressions using all possible combinations of operators (+, -, and nothing) except for starting with a "+".

The allexpr function evaluates all the generated expressions and counts the number of times each unique value appears in the results. It returns a dictionary with the values as keys and the counts as values.

The sumto function filters the generated expressions to find those that evaluate to a specific value (in this case, 100), and returns a vector of those expressions.

The maxsolve function finds the values that have the maximum number of solutions and the corresponding number of solutions. It does this by filtering the dictionary ae (created by allexpr) for values with the maximum count.

The minsolve function finds the smallest number that does not appear as a key in ae, indicating that there are no expressions that evaluate to that number.

The highersums function finds the highest sums that can be represented by the generated expressions and returns a sorted vector of these sums in descending order.

Finally, the program prints out the results:

  1. The expressions that evaluate to 100
  2. The values with the maximum number of solutions and the corresponding number of solutions
  3. The smallest number with no solutions
  4. The 10 highest sums that can be represented by the expressions

Source code in the julia programming language

using Printf, IterTools, DataStructures
 
expr(p::String...)::String = @sprintf("%s1%s2%s3%s4%s5%s6%s7%s8%s9", p...)
function genexpr()::Vector{String}
    op = ["+", "-", ""]
    return collect(expr(p...) for (p) in Iterators.product(op, op, op, op, op, op, op, op, op) if p[1] != "+")
end
 
using DataStructures
 
function allexpr()::Dict{Int,Int}
    rst = DefaultDict{Int,Int}(0)
    for e in genexpr()
        val = eval(Meta.parse(e))
        rst[val] += 1
    end
    return rst
end
 
sumto(val::Int)::Vector{String} = filter(e -> eval(Meta.parse(e)) == val, genexpr())
function maxsolve()::Dict{Int,Int}
    ae = allexpr()
    vmax = maximum(values(ae))
    smax = filter(ae) do (v, f)
        f == vmax
    end
    return smax
end
function minsolve()::Int
    ae = keys(allexpr())
    for i in 1:typemax(Int)
        if i ∉ ae
            return i
        end
    end
end
function highestsums(n::Int)::Vector{Int}
    sums = collect(keys(allexpr()))
    return sort!(sums; rev=true)[1:n]
end
 
const solutions = sumto(100)
const smax = maxsolve()
const smin   = minsolve()
const hsums = highestsums(10)
 
println("100 =")
foreach(println, solutions)

println("\nMax number of solutions:")
for (v, f) in smax
    @printf("%3i -> %2i\n", v, f)
end
 
println("\nMin number with no solutions: $smin")
 
println("\nHighest sums representable:")
foreach(println, hsums)


  

You may also check:How to resolve the algorithm Steffensen's method step by step in the ATS programming language
You may also check:How to resolve the algorithm Guess the number/With feedback (player) step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Literals/Floating point step by step in the Go programming language
You may also check:How to resolve the algorithm Text processing/1 step by step in the Vedit macro language programming language
You may also check:How to resolve the algorithm Monte Carlo methods step by step in the F# programming language