We’re rewarding the question askers & reputations are being recalculated! Read more.
27

Python 2, 171 156 bytes lambda a:max(e(a))-min(e(a)) u=')%s(' def e(a,t=u): try:b=[eval(a)] except:b=[] return sum([e('(%s)'%a.replace(o,t%o),u%t)for o in"+-*/%"if' '+o in a],b) Try it online! How it works We surround each operator with a different number of outward-facing pairs of parentheses to simulate different precedences (in all possible ways), ...


22

Jelly, 17 15 14 bytes 109876:54+3_21 Try it online! How it works 109876:54+3_21 109876 Initialize the left argument as 109876. :54 Perform integer division by 54, yielding 2034. +3 Add 3, yielding 2037. _21 Subtract 21, yielding 2016.


17

Python 2, 74 70 bytes f=lambda n:min([n]+[f(j)+min(n%j*n+f(n/j),f(n-j))for j in range(2,n)]) Try it online! Alternate version, 59 bytes (unverified) f=lambda n:min([n]+[f(j)+f(n/j)+f(n%j)for j in range(2,n)]) This works at least up to n = 1,000,000, but I have yet to prove that it works for all positive n. Try it online!


16

Retina, 58 bytes [-+] $&1 \B((\+1)|(-1))* $._$*1$#3$*1$#2$*_$& +`1_ 1+ $.& Try it online! Alternative solution at the same byte count: ((\+)|(-))* $._$*1$#3$*1$#2$*_$& +`1_ ([+-])1* $+1 1+ $.& Try it online! Explanation The basic idea is to turn all the +s and -s into simple +1 and -1 operations and then to prepend a sufficiently ...


10

JavaScript (ES6), 153 176 bytes EDIT: In non-strict mode, JS interprets 0-prefixed numerical expressions as octal (e.g. 017 is parsed as 15 in decimal). This is a fixed version that supports leading zeros. let f = s=>[...Array(3**(l=s.length,l-1))].map((_,n)=>m=eval((x=s.replace(/./g,(c,i)=>c+['','+','-'][o=(n/3**i|0)%3,j-=!o,o],j=l))....


10

Jelly, 16 14 bytes Thanks Dennis for saving 2 bytes! ÆḌḊ,Ṗ߀€+U$FṂo Try it online! Logic explanation Given a number n: If it's 1, the answer is 1. Otherwise: The representation is either a + b or a × b, where a and b are expressions. Consider all possible values of a and b: If the representation is a + b, then a and b are in range [1 .. n-1]. If the ...


9

85, ~2400 bytes I'm a bit sad this is a code golf challenge, as I feel that all my previous efforts have been rather useless now that I'll post this: 0 = ((2*0)^15) 1 = ((2^0)^15) 2 = (2-(0^15)) 3 = (20*.15) 4 = (20*(1/5)) 5 = (20-15) 6 = ((.20+1)*5) 7 = ((20*.1)+5) 8 = (2*((0-1)+5)) 9 = ((.20/.1~)*5) 10 = (20/(1/.5)) 11 = ((((2-0)+1))...


9

Hexagony, 888 bytes Okay, first some ground rules for Hexagony, in case anyone wants to beat this: I'm interpreting "snippet" as a linear piece of code that can be dumped into any sufficiently large program, provided the current and adjacent memory edges are zero. The snippet has to be entered from the left and exited from the right. I'd be able to save ...


8

Hexagony, 61 bytes Not gonna win, but I just wanted to do a challenge in Hexagony. This uses a different method than other answers (much worse). It takes some factors of 2016 (2,3,6,7,8) and multiplies them all together. Minified: \109.8/7}_=\"6<}{>...$_5_4/*!@...../}3.."2\/="*=}<*...$1>"*"/ Unminified: \ 1 0 9 . 8 / 7 } _ = \ " 6 &...


8

J, 1041 ... 838 bytes 981 961 952 860 859 I got a little lazy to the end, but it should be more fixed than less. I don't think I'll ever overtake Hexagony, but you never know! beating hexagony! Saved 9 bytes thanks to Zgarb! and so much more to Lynn! 20=16 *2016 2[016 2+01[6 20-16 p:2[016 201]6 2+0-1-6 -:20]16 2+01+6 -:20[16 p:20-16 +/2$01]6 <:20-1]6 ...


8

Python 2, 76 bytes lambda e:sum([[len(e+s)-2*s.count('+')]+[1]*len(s)for s in e.split('=')],[]) Try it online!


8

Jelly, 126 bytes "Operator Precedence? Parentheses? Pah, who needs that?" - challenges of using Jelly for an operator precedence challenge. ⁾[]i$€Ḥæ%3+\¬œp¹Ḋ€ ǵḟØDO%9µÐṀṪɓœṣ⁹,ṚÑj@¥/ ǵVṾµ1ĿFḟØDḟ”-Lµ?ÐL 5Ḷx@€“]“[”ż⁸j/€,@y³Fɓ³i@€Ṁ’x@“[“]”jÇ “+_×:%”Œ!Ç€µṾL_L’ỊµÐfV€ṢIS Try it online! Input is taken as a string, e.g. "1+2_3×4:5%6". Note multiplication uses "×...


7

Python 2.7 (284), Python 3.x (253) from __future__ import division #(Remove for Python 3.x) from itertools import * a=raw_input().split() for i in permutations(a[:-1],5): for j in product('+-*/',repeat=5): for k,l in combinations(range(1,12,2),2): d=''.join(sum(zip(i,j),()))[:-1];d='('+d[:l]+')'+d[l:] if eval(d)==int(a[-1]):print d;b It gives an ...


7

Python 155 bytes h={4:'4',24:'4!',6:'â4',.4:'.4',1:'âû4',4/9.:'.4~'} f={} def g(r,s='24',y='4!'):f[eval(s)]=y;[g(r-1,s+o+`k`,y+o+h[k])for k in h for o in'/*-+'if r] g(3) The first three bytes (\xEF\xBB\xBF) are the UTF-8 byte order mark, although the file should be saved in an ANSI format. The û and â will be interpretted as √ and Γ respectively in ...


7

JavaScript, 1021 bytes Fixed and saved two bytes thanks to Charlie Wynn and ETHProductions. 201&6 -~!2016 2%016 201%6 20%16 2^0^1^6 2*0*1+6 2|0|1|6 2*01+6 2-~01+6 ~2+016 ~2+016 2^016 20-1-6 2|016 20+1-6 20&16 2-~016 2.0+16 20^1+6 20|16 -~20|16 20*1|6 20|1|6 -2*~01*6 20-1+6 20+1*6 20+1+6 2*016 -~(2*016) 2*-~016 ~-(2.0*16) 2.0*16 -~(2.0*16) 2.0*-~16 ~...


7

JavaScript (ES7), 836 bytes Everything should work in any browser except 81, 88, and 97, which use the new ** operator. Mostly everything here was done by hand. I've been working on a brute-forcer to improve anything that can be improved. Currently it has saved 103 bytes on various items. 0: 201&6 1: 2-01%6 2: 2%016 3: 201%6 4: 20%16 5: ...


7

Jelly, 686 bytes 20=16 20>16 2+0%16 201%6 20%16 20%16‘ 201a6 20>1+6 20%16Ḥ 2016DS 20Ho16 2016BL 20:1.6 20_1_6 20×1_6 20+1_6 20&16 20:+16 2+0+16 20+1^6 20|16 20|16‘ 20|16‘‘ 20%16!’ 20%16! 20²:16 20H+16 20+1+6 20×1_6Ḥ 20×1_6Ḥ‘ 20+1_6Ḥ 2016&½’ 2016&½ 201:6 201:6‘ 20Cạ16 20+16 20+16‘ 20+1^6Ḥ 20|16Ḥ’ 20|16Ḥ 20|16Ḥ‘ 20|16‘Ḥ 2016½Ḟ’ 2016½Ḟ 2016½Ċ ...


7

Haskell, 42 bytes f n=[l|l<-mapM(\i->[i,-i])[1..n],0==sum l] Try it online!


6

GolfScript (129 chars*) [4.`2'√4'24'4!'6'Γ4'1'Γ√4'120'ΓΓ4']2/:F{.F=[[44.`]]*\{`{+{'+*-'1/{:^;.[~@[\]{'()'1/*}%^*@@^~\]\}/}:|~2/~\+|;}+F/}%+}3*\{\0==}+?1= Running time is on the order of 4 minutes on my PC. A moderate speed-up can be obtained at the cost of two characters by adding a uniqueness operation .& immediately after the %+. I use pre-coded ...


6

Python 3, 424 420 369 363 bytes import time as t r=range x=len d=list(t.strftime('%m%d%y')) o=([[x,x+'(',x+')']for x in ['']+"+ - == * / **".split()]) n=[] for l in o: n=l+n o=n for p in r(x(o)**(x(d)-1)): e='' for i in r(x(d)-1): e+=str(d[i])+o[(p//(x(o)**i))%x(o)] e+=str(d[-1]) try: if eval(e)and e.find('=')!=-1: ...


6

Python 2, 199 179 178 172 162 158 156 152 151 bytes Way too long, but the solution was easy to create. from itertools import* i=input() k='%s' s=k+k.join(i)+k for p in product(*[range(1,65537)]*-~len(i)): if eval((s%p).replace('=','==')):print s%p;break Try it online This will try every possibility until it finds a solution. The program is extremely ...


6

Python 2, 235 234 233 226 bytes -1 byte (and a fix) thanks to Anders Kaseorg! -7 bytes thanks to Step Hen! from itertools import* def f(e,a=()): for o in permutations("+-*/%"): l=e[:] for c in o: for i in range(len(l),0,-1): if l[i-1]==c:l[i-2:i+1]=["("+l[i-2]+l[i-1]+l[i]+")"] try:a+=eval(*l), except:0 print max(a)-min(a) ...


5

C++17, score .0086 This program has non-deterministic penalty score due to thread races, so I'm declaring based on an average of three runs, each of which handled the test suite in under a minute: score 0.000071 for 14(11*13) = 14143 score 0.000019 for (696699+66)*69 = 48076785 score 0.000069 for 333333+333333333+33333 = 333699999 score 0.000975 for 5(1((((...


5

Pyth, 16 bytes +/109876 54-3 21 Does integer division, then adds (3-21). Try it here.


5

Javascript (E6) 215 (315 - 2*50 bonus) 252 Edit Simplified. Correct bug of 0 missing Defined as a function, then counting 10 more byte for output using alert() Important Really this one is not valid according to the rules, because javascript can not handle big numbers. For instance with parameters 2,5 it can't find 2^2^2^2^2 (ie 2^65536). A number this ...


5

Python 2, 120 119 bytes -1 byte thanks to mbomb007 a=['1'+(n and('1'.join(n)+'1'))for n in input().split('=')] print'='.join(`max(map(eval,a))-eval(c)+1`+c[1:]for c in a) Try it online! or Verify all test cases First I insert 1 in every position, to check the highest value, then add it as offset on every equation. This work because you can't add ...


5

JavaScript (ES6), 92 82 bytes Golfed 8 bytes with a trick from @xnor let f = x=>x.split`=`.map(q=>(x+q).length-2*~-q.split`+`.length+[...q,''].join(1)).join`=` <input oninput="if(/^[+=-]+$/.test(value))O.innerHTML=f(value)" value="="><br> <pre id=O>1=1</pre> The trick here is to insert a 1 after every + or -, then ...


5

MATL, 37 36 bytes n'+-'OhZ^!t2\s&SZ)"G@!vXzU100=?@z3M. The test case takes about 6 seconds in TIO. Try it online! How it works n % Implicitly input a string. Number of elements, say k '+-' % Push this string Oh % Append char 0. This is treated like ' ' (space) Z^ % Cartesian power of the three-char string '+- ' raised to k. ...


5

Pari/GP, 66 bytes A port of Dennis's Python answer: f(n)=vecmin(concat(n,[f(j)+min(n%j*j+f(n\j),f(n-j))|j<-[2..n-1]])) Try it online! Pari/GP, 72 bytes Longer, but more efficient: f(n)=if(n<6,n,vecmin([if(d>1,f(d)+f(n/d),1+f(n-1))|d<-divisors(n),d<n])) Try it online!


Only top voted, non community-wiki answers of a minimum length are eligible