Home Puzzles arithmetic – Make numbers 1 – 40 utilizing digits 2, 0, 2, 1

arithmetic – Make numbers 1 – 40 utilizing digits 2, 0, 2, 1

0
arithmetic – Make numbers 1 – 40 utilizing digits 2, 0, 2, 1

[ad_1]

Assuming

that “you may concatenate” means not solely the person digits, but in addition the outcomes of their operations,

the out there numbers are

0 to 12, 14 to 25, 28, 30, 32, 38, 39 and 40.

due to

the next Python script (sorry for such a clumsy one!) which enumerate all doable binary timber with not more than 4 leaves and computes the end result for expression denoted by every of the timber for all permutations of operators and operands (that are 2021 digits)

from itertools import permutations, groupby
from operator import itemgetter

# all permutations of 2021 digits
p = listing(permutations([2, 0, 2, 1]))
# all operations
ops = [("+", lambda x, y: x + y), 
       ("-", lambda x, y: x - y), 
       ("*", lambda x, y: x * y),
       ("/", lambda x, y: x / y if  y != 0 else 1000000),
       ("|", lambda x, y: float(str(abs(int(x))) + str(abs(int(y)))))]

# set of values
v = set()

for x in p:
    # utilizing 1 digit:
    v.add((f"{x[0]}", x[0]))
   # utilizing 2 digits:
    for (s1, op1) in ops:
        v.add((f"{x[0]} {s1} {x[1]}", op1(x[0], x[1])))
    # utilizing 3 digits:
    for (s1, op1) in ops:
        for (s2, op2) in ops:
            v.add((f"({x[0]} {s1} {x[1]}) {s2} {x[2]}", op2(op1(x[0], x[1]), x[2])))
            v.add((f"{x[0]} {s1} ({x[1]}) {s2} {x[2]})", op1(x[0], op2(x[1], x[2]))))
    # utilizing 4 digits:
    for (s1, op1) in ops:
        for (s2, op2) in ops:
            for (s3, op3) in ops:
                v.add((f"({x[0]} {s1} {x[1]}) {s2} ({x[2]} {s3} {x[3]})", op2(op1(x[0], x[1]), op3(x[2], x[3]))))
                v.add((f"(({x[0]} {s1} {x[1]}) {s2} {x[2]}) {s3} {x[3]}", op3(op2(op1(x[0], x[1]), x[2]), x[3])))
                v.add((f"(({x[0]} {s1} ({x[1]} {s2} {x[2]})) {s3} {x[3]}", op3(op1(x[0], op2(x[1], x[2])), x[3])))
                
                v.add(( f"{x[0]} {s1} (({x[1]} {s2} ({x[2]}) {s3} {x[3]})", op1(x[0], op3(op2(x[1], x[2]), x[3]))))

outcomes = listing(r for r in v if 0 <= r[1] <= 40 and int(r[1]) == r[1])
outcomes.type(key=itemgetter(1))

abridged = [list(g)[0] for okay, g in groupby(outcomes, key=itemgetter(1))]

for s in abridged: print(s)

(Try it on-line!)

Note that each one numbers not talked about in hexomino’s reply use concatenation of outcomes (denoted right here by $#$):

$15= ((1 + 2) # 0) / 2$
$25 = 2 # (10 / 2)$
$28 = (1 + 2) # 0 – 2$
$30 = (1 + 2) # 0$

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here