# It’s time to do the math

## Python (imperfect) ~~493~~ 474 - 300 = 174

There are a fair number of issues with this solution, firstly that it ignores any exponent that is too large (any in which the exponent is greater than 100). I actually don't think this removes any possibilities for inputs less than or equal to 5, but I'm not 100% sure.

Another thing is that it does not consider any unary square roots, as it would get complicated (any solution with any term equal to 0 or 1 would produce an infinite number of solutions). It also does not consider any unary negation (the '-' symbol) for the same reason, as well as the fact that I'm not actually sure if the question asked for it.

I also considered what criteria should decide if two expressions were equivalent, but I couldn't find a way to rigorously define it in a way I found to be intuitive, so (for now at least) I didn't implement anything like that. This does mean that it outputs quite a few results, and it also uses parenthesis in a fairly naive way.

On a side note I think that this might include the longest single line of code I've written, especially before it was fully golfed.

```
R=range
F=lambda s:lambda a,b:eval(s)
L=lambda D,N:[(int(str(D)*N),str(D)*N)]+[(o(u,v),"(%s%s%s)"%(s,c,t))for p in R(1,N)for u,s in L(D,p)for v,t in L(D,N-p)for c,o in[('+',F('a+b')),('-',F('a-b')),('*',F('a*b')),('/',F("1.*a/b if b else''")),('^',F("''if(a<0 and int(b)!=b)|(a and b<0)|(b>99)else a**b")),('v',F("b**(1./a)if a and(a>=0 or b)and(b>=0 or int(1./a)==1./a)&(1./a<99)else''"))]if o(u,v)!='']
A=L(*input())
for i in R(11):
for v,s in A:
if v==i:print i,s[1:-1]
```

Example: ('v' represents '√')

```
2,3
0 2*(2-2)
0 2v(2-2)
0 (2-2)*2
0 (2-2)/2
0 (2-2)^2
1 2^(2-2)
1 2-(2/2)
1 2v(2/2)
1 (2/2)^2
2 2v(2+2)
2 2+(2-2)
2 2-(2-2)
2 2v(2*2)
2 2*(2/2)
2 2/(2/2)
2 2^(2/2)
2 2v(2^2)
2 (2+2)-2
2 (2+2)/2
2 (2-2)+2
2 (2*2)-2
2 (2*2)/2
2 (2/2)*2
2 (2/2)v2
2 (2^2)-2
2 (2^2)/2
3 2+(2/2)
3 (2/2)+2
6 2+(2+2)
6 2+(2*2)
6 2+(2^2)
6 (2+2)+2
6 (2*2)+2
6 (2^2)+2
8 2*(2+2)
8 2*(2*2)
8 2*(2^2)
8 (2+2)*2
8 (2*2)*2
8 (2^2)*2
```

### Python 3 – ~~349~~ 346

```
r=range
l=lambda s:eval("lambda a"+s)
def T(u,f,X,Y):
try:return u(f(X,Y))
except:0
c=l(',x:{x}.union(*[{u(int("1"*a)*x)}|{T(u,f,X,Y)for j in r(1,a)for X in c(j,x)for Y in c(a-j,x)for f in[l(",b:a%sb"%o)for o in{"**"}|set("+-*/")]+[l(",b:a**b**-1")]}for u in[l(":-a")]+[l(":a**.5**%i"%k)for k in r(9)]])')
R=l(",i:[{n+1}<c(i,a)for n in r(10)]")
```

Here is a rather ungolfed version:

```
def R(x,i):
# Unary Operations
U = [lambda a:-a] + [eval("lambda a:a**(1/2.**%i)" % j) for j in range(9)]
# Binary Operations
F = [eval("lambda a,b:a%sb"%o) for o in ["+","-","*","/","**"]] + [lambda a,b:a**(1./b)]
def combos(i):
L = {x}
for u in U:
# 3, 33, 333, etc.
L |= {u(int(str(x)*i))}
for j in range(1,i):
for X in combos(j):
for Y in combos(i-j):
for f in F:
# To avoid trouble with division by zero, overflows and similar:
try:
L |= {u(f(X,Y))}
except:
pass
return L
return [n in combos(i) for n in range(1,11)]
```

For testing I recommend to change `(9)`

to something smaller, since this is the number of multiple square roots taken into account, which has a huge impact on the performance.

Finally, this made me wonder, whether the unary minus is actually needed in some case …

### Mathematica - 246 characters (no bonuses claimed)

```
f[x_,y_]:=x-y
g[x_,y_]:=x/y
h[x_,y_]:=x^(1/y)
j[x_,y_]:=FromDigits@Join[IntegerDigits@x,{y}]
z[{r_,n_,L_}]:=z[{L[[1]][r,n],n,Rest@L}]
z[{r_,n_,{}}]:=r
a[n_,t_]:=Union@Select[z[{n,n,#}]&/@Tuples[{Plus,f,Times,g,Power,h,j},t-1],IntegerQ@#&&0<#<11&]
```

### Explanation

Function `j`

concatenates two numbers digit-wise.

Function `z`

takes a result `r`

, number `n`

, and list of functions `L`

, each which operates on two arguments. It then applies the list of functions sequentially to argumnts `[r,n]`

using recursion, until the list is empty, whereupon it returns the result.

Function `a`

takes a number `n`

and a number of copies `t`

. It creates all tuples of length (t-1) from the list of functions `{Plus, f, Times, g, Power, h, j}`

and sends each tuple through function z, then returns a list of all numbers 1 through 10 that were created.

Example execution `a[2,3]`

returning `{1, 2, 3, 6, 8}`

.

### Limitations

Because the list of functions is applied sequentially, consuming one copy of the number each time, it can miss some combinations. For example, when operating on four twos, it would miss 22/22 = 1 due to its inability to evaluate the list of functions out of order. Of course, 2/2*2/2 = 1 covers this case.