# Integer Complexity

Your task is to write a program that, on input n, outputs the minimal expression of each number 1 through n in order. The shortest program in bytes wins.

A minimal expression combines 1's with addition and multiplication to result in the given number, using as few 1's as possible. For example, 23 is expressed as 23=((1+1+1)(1+1)+1)(1+1+1)+1+1 with eleven ones, which is minimal.

Requirements:

1. The program must take as input a positive natural number n.
2. Output must be in this format: 20 = ((1+1+1)(1+1+1)+1)(1+1)
3. Your output may not have needless parentheses, like 8 = ((1+1)(1+1))(1+1).
4. The multiplication sign * is optional.
5. Spaces are optional.
6. You don't have to output all the possible equations for given value: For example, you have the choice to output 4=1+1+1+1 or 4=(1+1)(1+1). You don't have to output both.
7. The shortest program (in bytes) in each language wins.
1=1
2=1+1
3=1+1+1
4=1+1+1+1
5=1+1+1+1+1
6=(1+1+1)(1+1)
7=(1+1+1)(1+1)+1
8=(1+1+1+1)(1+1)
9=(1+1+1)(1+1+1)
10=(1+1+1)(1+1+1)+1
11=(1+1+1)(1+1+1)+1+1
12=(1+1+1)(1+1)(1+1)
13=(1+1+1)(1+1)(1+1)+1
14=((1+1+1)(1+1)+1)(1+1)
15=(1+1+1+1+1)(1+1+1)
16=(1+1+1+1)(1+1)(1+1)
17=(1+1+1+1)(1+1)(1+1)+1
18=(1+1+1)(1+1+1)(1+1)
19=(1+1+1)(1+1+1)(1+1)+1
20=((1+1+1)(1+1+1)+1)(1+1)


Here are some more test cases: (remember, that other expressions with the same number of 1's are also allowed)

157=((1+1+1)(1+1)(1+1)+1)(1+1+1)(1+1)(1+1)+1

444=((1+1+1)(1+1+1)(1+1)(1+1)+1)(1+1+1)(1+1)(1+1)

1223=((1+1+1)(1+1+1)(1+1+1)(1+1+1)(1+1+1)+1)(1+1+1+1+1)+1+1+1

15535=((((1+1+1)(1+1+1)(1+1+1)(1+1+1)+1)((1+1+1)(1+1)+1)+1)(1+1+1)+1)(1+1+1)(1+1+1)+1

45197=((((1+1+1)(1+1)(1+1)(1+1)+1)(1+1+1+1+1)(1+1)+1)(1+1+1)(1+1)(1+1)+1)(1+1+1+1+1)(1+1+1)+1+1


Good Luck! - The Turtle 🐢

• 1) Your bullet #6 is not finished (it's missing the example output for n=20) and 2) you say at the beginning that the integer complexity, which is distinct from the equation, has to be output, but you don't include that in any of the examples except the very first one. – El'endia Starman Nov 17 '15 at 0:56
• I'm still not clear. Do you just output the equation? – xnor Nov 17 '15 at 1:07
• Yes. The integer complexity should not be outputted. I will also clarify that. Sorry for the mistakes. :( – The Turtle Nov 17 '15 at 1:08
• Whoops, I said bullet #6 when I should've said bullet #5, in your requirements list. As for the other issue, thanks for fixing it. :) – El'endia Starman Nov 17 '15 at 1:14
• Related: oeis.org/A005245 oeis.org/A061373 and finally oeis.org/A091333 – flawr Nov 17 '15 at 16:28

# Pyth, 60 bytes

LjWqeb\1b()L?tbho/N\1++'tb"+1"m+y'/bdy'df!%bTr2b1VSQ++N\='N


Demonstration

The online compiler can reach 1223 before the time out, thanks to Pyth's automatic function memoization.

1223=((1+1+1)(1+1+1)(1+1+1)(1+1+1)(1+1+1)+1)(1+1+1+1+1)+1+1+1


In abbrieviated notaion,

1223=(3^5+1)*5+3


This uses a recursive function ', which calculates all possbile products and sums which could give the desired output, finds the shortest string with each final operation, then compares them by 1 count and returns the first one.

It uses a helper function, y, which parenthesizes an expression only if it needs to be parenthesized.

Offline, I am running the program with the input 15535, and it is nearly complete. Results are printed incrementally, so it is easy to see the progress.

Final lines of the output:

15535=((((1+1+1)(1+1+1)(1+1+1)(1+1+1)+1)((1+1+1)(1+1)+1)+1)(1+1+1)+1)(1+1+1)(1+1+1)+1

real    7m8.430s
user    7m7.158s
sys 0m0.945s


In abbreviated notation,

15535=(((3^4+1)*(3*2+1)+1)*3+1)*3^2+1


# CJam, 10510298 96 bytes

q~{)'=1$2,{:I{I1$-'+}%3/1>Imf'*+aImp!*+{)\{j}%\+}:F%{e_"+*"-:+}$0=}j2,{F)_'*={;{'(\')}%1}&*}jN}/  Try it online in the CJam interpreter. ### Test run The online interpreter is too slow for the larger test cases. Even with the Java interpreter, the larger test cases will take a long time and require a significant amount of memory. $ time cjam integer-complexity.cjam <<< 157
1=1
2=1+1
3=1+1+1
4=1+1+1+1
5=1+1+1+1+1
6=(1+1)(1+1+1)
7=1+(1+1)(1+1+1)
8=(1+1)(1+1)(1+1)
9=(1+1+1)(1+1+1)
10=1+(1+1+1)(1+1+1)
11=1+1+(1+1+1)(1+1+1)
12=(1+1)(1+1)(1+1+1)
13=1+(1+1)(1+1)(1+1+1)
14=(1+1)(1+(1+1)(1+1+1))
15=(1+1+1)(1+1+1+1+1)
16=(1+1)(1+1)(1+1)(1+1)
17=1+(1+1)(1+1)(1+1)(1+1)
18=(1+1)(1+1+1)(1+1+1)
19=1+(1+1)(1+1+1)(1+1+1)
20=(1+1)(1+1)(1+1+1+1+1)
21=(1+1+1)(1+(1+1)(1+1+1))
22=1+(1+1+1)(1+(1+1)(1+1+1))
23=1+1+(1+1+1)(1+(1+1)(1+1+1))
24=(1+1)(1+1)(1+1)(1+1+1)
25=1+(1+1)(1+1)(1+1)(1+1+1)
26=(1+1)(1+(1+1)(1+1)(1+1+1))
27=(1+1+1)(1+1+1)(1+1+1)
28=1+(1+1+1)(1+1+1)(1+1+1)
29=1+1+(1+1+1)(1+1+1)(1+1+1)
30=(1+1)(1+1+1)(1+1+1+1+1)
31=1+(1+1)(1+1+1)(1+1+1+1+1)
32=(1+1)(1+1)(1+1)(1+1)(1+1)
33=1+(1+1)(1+1)(1+1)(1+1)(1+1)
34=(1+1)(1+(1+1)(1+1)(1+1)(1+1))
35=(1+1+1+1+1)(1+(1+1)(1+1+1))
36=(1+1)(1+1)(1+1+1)(1+1+1)
37=1+(1+1)(1+1)(1+1+1)(1+1+1)
38=(1+1)(1+(1+1)(1+1+1)(1+1+1))
39=(1+1+1)(1+(1+1)(1+1)(1+1+1))
40=(1+1)(1+1)(1+1)(1+1+1+1+1)
41=1+(1+1)(1+1)(1+1)(1+1+1+1+1)
42=(1+1)(1+1+1)(1+(1+1)(1+1+1))
43=1+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(1+(1+1)(1+1+1)(1+1+1+1+1))
156=(1+1)(1+1)(1+1+1)(1+(1+1)(1+1)(1+1+1))
157=1+(1+1)(1+1)(1+1+1)(1+(1+1)(1+1)(1+1+1))

real    0m3.896s
user    0m4.892s
sys     0m0.066s


Given enough time, it would produce these solutions for the next test cases:

444=(1+1)(1+1)(1+1+1)(1+(1+1)(1+1)(1+1+1)(1+1+1))
1223=1+1+(1+1+1)(1+1+(1+1+1)(1+1+1))(1+(1+1)(1+1)(1+1+1)(1+1+1))

• How does it work? – flawr Nov 17 '15 at 10:35
• @flawr I'm still hoping to golf this a bit more. I'll add an explanation when I am done/give up. – Dennis Nov 17 '15 at 16:11

# Julia, 229 bytes

n->(F=i->K[i]>0?E[i]:"("E[i]")";C=[1;3:n+1];K=0C;E=fill("1",n);for s=1:n for i=1:s÷2 (D=C[i]+C[s-i])<C[s]?(C[s]=D;E[s]=E[i]"+"E[s-i];K[s]=0):s%i>0||(D=C[i]+C[j=s÷i])<C[s]&&(C[s]=D;E[s]=F(i)F(j);K[s]=1)end;println("\$s="E[s])end)


This is actually pretty fast. Assigning the function to f and running @time f(15535) gives the output (last two lines only)

15535=1+(1+1+1)(1+1+1)(1+(1+1+1)(1+(1+(1+1)(1+1+1))(1+(1+1+1)(1+1+1)(1+1+1)(1+1+1))))
32.211583 seconds (263.30 M allocations: 4.839 GB, 4.81% gc time)


and for @time f(45197), it gives

45197=1+1+(1+1+1)(1+1+1+1+1)(1+(1+1)(1+1)(1+1+1)(1+(1+1)(1+1+1+1+1)(1+(1+1)(1+1)(1+1)(1+1+1))))
289.749564 seconds (2.42 G allocations: 43.660 GB, 4.91% gc time)


So, what's the code doing? Simple - C holds the current one-Count for the number, K is an indicator array keeping track of whether the expression is, fundamentally, a sum or a product, for the purposes of dealing with bracketing, and E holds the Expression itself. Working its way up from s=1 through to n, the code searches for the minimal representation of number s in terms of lower values, by looking for either a sum or a product. If it's a product, then it checks the two components and puts brackets around them if they're sums. That check is done in function F`, to save bytes (because it has to be done twice, for the two factors).