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

Published on 12 May 2024 09:40 PM

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

Source code in the picat programming language

main =>
    L = "23456789",
    gen(L,Str),
    Exp = parse_term(['1'|Str]),
    Exp =:= 100,
    println(['1'|Str]).

gen(L@[_],Str) => Str = L.
gen([D|Ds],Str) ?=> Str = [D|StrR], gen(Ds,StrR).     % no operator
gen([D|Ds],Str) ?=> Str = ['+',D|StrR], gen(Ds,StrR). % insert +
gen([D|Ds],Str) => Str = ['-',D|StrR], gen(Ds,StrR).  % insert -

  

You may also check:How to resolve the algorithm Copy a string step by step in the Maple programming language
You may also check:How to resolve the algorithm Determine if a string is collapsible step by step in the C programming language
You may also check:How to resolve the algorithm Department numbers step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Jump anywhere step by step in the Logtalk programming language
You may also check:How to resolve the algorithm Read entire file step by step in the BQN programming language