| Bytes | Lang | Time | Link |
|---|---|---|---|
| 192 | Maple | 250503T025922Z | dharr |
| 054 | Charcoal | 250503T001222Z | Neil |
| 021 | 05AB1E | 250501T135019Z | Kevin Cr |
| 219 | Python 3 | 250501T090634Z | CaydendW |
Maple, 192 bytes
proc(s)n:=nops(s);X:=[x||($n)];{numer(normal(parse(cat(seq(cat("",X[i],s[i]),i=1..n-1),X[n]-s[n]))))=0,seq(seq(X[i]<>X[j],i=1..j-1),j=1..n)};q:=SMTLIB:-Satisfy(%)assuming posint;eval(X,q)end;
proc(s)
n:=nops(s); # number of variables
X:=[x||($n)]; # variable names x1,x2,...,xn
# next line assembles a string with the expression, and parses it
{numer(normal(parse(cat(seq(cat("",X[i],s[i]),i=1..n-1),X[n]-s[n]))))=0,
# conditions to ensure all variables are distinct
seq(seq(X[i]<>X[j],i=1..j-1),j=1..n)};
q:=SMTLIB:-Satisfy(%)assuming posint; # find a solution
eval(X,q) # output as list
end;
Uses the SMTLIB solver to find a solution. In principle, it should work by solving the original expression, but this only worked for the simplest cases. This problem was solved by the putting over a common denominator and solving for the numerator (a multivariate polynomial) to be equal to zero (numer(normal(...)).
Charcoal, 54 bytes
≔⁺θ-θWUV⁺ωη«→≔Eθ↨²﹪↨÷ⅈX²λX²Lθ²υ¿∧⌊υ⬤υ⁼¹№υκ≔⭆υ⁺κ§θλω»Iυ
Try it online! Link is to verbose version of code. Takes a string of arithmetic operators as the first input. Explanation:
≔⁺θ-θ
Append a - to the string of operators.
WUV⁺ωη«
Repeat until a solution is found to (equation - target) equals zero.
→≔Eθ↨²﹪↨÷ⅈX²λX²Lθ²υ
Generate another list of integers.
¿∧⌊υ⬤υ⁼¹№υκ
Check that the integers are positive and distinct.
≔⭆υ⁺κ§θλω
If so then create a Python expression for the equation with the integers inserted, plus a trailing - (which will subtract the target).
»Iυ
Output the found integers.
05AB1E, 21 bytes
∞æIgù€œ€`.ΔI.ι`UJ.EXQ
Try it online or verify all test cases.
Explanation:
∞ # Push an infinite positive list: [1,2,3,...]
æ # Get the powerset of this: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3],...]
Ig # Push the input-length
ù # Only leave lists of that length
€ # Map over each list:
œ # Get all permutations
€` # Flatten it one level down to a list of lists
.Δ # Pop and find the first inner list that's truthy for:
I.ι # Interleave the current list and input-list
` # Pop and push all values separately to the stack
U # Pop and store the last value in variable `X`
J # Join all other values on the stack to a string
.E # Evaluate this expression as Elixir-code
XQ # Check if the result of the equation equals `X`
# (after which the first valid result is output implicitly)
Python 3, 219 bytes
def f(x):
g=i=1;n=len(x)
while 1:
l=[i//g**j%g for j in range(n)]
if len(set(l))==len(l) and all(l) and eval(''.join([v for p in zip(list(map(str,l)),x[:n-1]+[''])for v in p]))==x[n-1]:return l
g+=any(l)^1;i+=1
This is my first python answer and I doubt it's very good. I assume a little more can be golfed off but I guess I'll get the ball rolling.
Explanation
The code brute forces a solution by generating lists of numbers that meet the criteria. Then the numbers are zipped into the list of operators, evaled and the result is checked. Not particularly complex.
It is also not very fast for obvious reasons.