Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As a spin-off to my challenge over at Puzzling, your goal is to output 2016.

Rules:

  • You must include the numbers 10 9 8 7 6 5 4 3 2 1 in that order. They can be used as individual integers or concatenated together (like 1098), but the 10 may not be separated into 1 and 0 - no character(s) may be present between the digits. Note that, in some languages, 10 may not actually represent the integer literal 10, which is acceptable.
  • Your code must not contain any other numbers or pre-defined number variables or constants (so T in Pyth is not allowed, since it is a numeric constant).
  • You must calculate 2016 using numerics. Simply outputting 2016 without performing any operations on the required numbers (for example, by decoding an encoded string consisting of only alphabetic characters) is not allowed. Outputting 2016 in pieces (such as 20, then 16) is also not allowed; you must have a single output consisting of the numeric value 2016.
  • The valid answer with the fewest bytes wins.
share|improve this question
3  
@nicael I'm pretty sure solving the puzzle has been done. We've had several of these "insert operators to solve an equation" challenges, but they are exceptional hard to search for. – Martin Ender Jan 6 at 13:40
1  
The previous revision (2) was more interesting actually. The new is just printing the string, the calculation was already made in your puzzling question... – nicael Jan 6 at 14:19
1  
Just a few questions based on what I can see on the current question: 1) Can we calculate 20 and 16 and print them one after the other or does the calculated number need to be 2016 before printing? 2) Are functions allowed? 3) Is concatenation of digits allowed? e.g. 1098 (I'm assuming yes by previous comments, but just to confirm) 4) Does "calculate 2016 using integers" mean that we can never have floats anywhere in an intermediate step? e.g. can I square root a number and round down? – Sp3000 Jan 6 at 23:17
1  
5) What happens if I have a language where "10" is not treated as the number ten, but rather a one followed by a zero and there was no way around it? Is such a language disqualified? (example language: Befunge) 6) Can we use a predefined number variable in place of 10, e.g. T987654321? – Sp3000 Jan 6 at 23:19
    
@Sp3000 1 No. 2 No. 3 Yes. 4 Floats are OK as long as you don't break any other rules. 5 10 must be included so you would need to handle that somehow. 6 As long as 10 appears before T. – rybo111 Jan 7 at 0:15

16 Answers 16

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.
share|improve this answer

Pyth, 16 bytes

+/109876 54-3 21

Does integer division, then adds (3-21).

Try it here.

share|improve this answer

Japt, 17 16 bytes

Â(109876/54-321q

I hate this 17. Probably will find another solution. YAYZ.

Explanation:

  • 321q is a square root of 321.
  • ~~ floors the number.

Try it online!

share|improve this answer
    
 == ~~ :-) – ETHproductions Jan 6 at 22:35
    
109876/54-321¬f is 15 :-D – ETHproductions Jan 6 at 22:36
    
@Eth but f doesn't work, no? – nicael Jan 6 at 22:37
    
It should be fixed. But the interpreter is down for maintenance right now, I'll get it back up momentarily. – ETHproductions Jan 6 at 22:38
    
109876/54-321q)f now works. The other suggestion doesn't. – ETHproductions Jan 6 at 22:47

bc, 14

109876/54+3-21

Nothing exciting here - borrows from other answers.

share|improve this answer
1  
The dc equivalent 109876 54/3+21-p scores 16, but doesn't warrant an answer of its own. – Toby Speight Jan 8 at 8:55

Haskell, 31 bytes

[10,9*8*7+const 6 5..]!!4+3*2*1

Not the shortest, 10+9*8-7+654*3-21 like seen in other answers works in Haskell too, but something different.

This builds a list starting with 10 and 9*8*7+6 = 510, so the offset is 500 for the following elements. The whole list is [10,510,1010,1510,2010,2510 ...]. We pick the 4th element (index 0-based), i.e. 2010 and add 3*2*1 = 6. Voilà.

I use const 6 5 = 6 to get rid of the 5 which is not needed.

share|improve this answer

TI-BASIC, 17 15 bytes

int(109876/54-√(321

This uses @nicael's method.

17 bytes:

10+9*8-7+654*3-21

This solution from Puzzling can be directly translated into TI-BASIC.

share|improve this answer
1  
Also valid in Japt, and probably some others. – ETHproductions Jan 6 at 19:02
1  
Also works in PowerShell, and Mathematica (Wolfram), and I would imagine many, many others. And probably works in dozens more with trivial modifications. – TimmyD Jan 6 at 19:19
    
A lovely polyglot solution – TanMath Jan 6 at 19:36
    
If you want to take the other languages, I'll delete my community wiki one. – VTCAKAVSMoACE Jan 6 at 19:39

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 < } { >
 . . . $ _ 5 _ 4
/ * ! @ . . . . .
 / } 3 . . " 2 \
  / = " * = } <
   * . . . $ 1
    > " * " /

Explanation coming soon;

share|improve this answer

Matlab / Octave, 23 bytes

(10-9+8-7)^(6-5+4)*3*21

Try it online

share|improve this answer

Math++, 17 bytes

_(109876/54)+3-21

Actually, this prints 2016.0. But there's really no way to print the exact string 2016 in this language.

The 17-byte TI-BASIC solution would also work here.

share|improve this answer

Polyglot, 17 Bytes

10+9*8-7+654*3-21

This code, first used in Thomas Kwa's TI-BASIC answer, also works in:

  • AppleScript (full program)
  • bc (full program)
  • Math++ (expression or full program)
  • Mathematica (function, therefore not valid)
  • Powershell (full program)
  • Japt (full program)
  • JavaScript (console input, therefore not valid) Needs second verification
  • Perl 5 (function, therefore not valid). Needs second verification
  • Haskell (function, therefore not valid)
  • Python REPL (expression, so REPL environment is needed to get the output)
share|improve this answer
1  
And, what's the point? – nicael Jan 6 at 19:34
    
@nicael I'm ---going--- am planning (unless Thomas Kwa wishes to add the other answers to his as well) to add all of the answers that involve this answer (except TI-BASIC) that I can find. Marked as Community so that others can contribute. – VTCAKAVSMoACE Jan 6 at 19:38
1  
Why the "function, therefore not valid" remarks? Functions are allowed by default. – nimi Jan 6 at 21:54
4  
I don't know about the other languages, but 10+9*8-7+654*3-21 is neither a JavaScript nor a Perl function. – Dennis Jan 6 at 21:56
1  
@Sp3000: Oh these invalidating rule changes ... – nimi Jan 7 at 0:50

𝔼𝕊𝕄𝕚𝕟 2, 15 chars / 17 bytes

109876/54+3-21⍜

Try it here (Firefox only).

Translates to round(109876/54+3-21).

Thanks to @Dennis for saving 2 bytes!

share|improve this answer

Vitsy, 29 bytes

I know this is sub-optimal (I thought we had to use all of the numbers at first, I'll update this with better solutions later).

a987654321-/*r***v{v}/v{v}/*N
a987654321                     Push 10, 9, 8..., 2, 1 to the stack (hexadecimal is the _only_ way to get 10 in this language, as 10 is pushing 1, then 0)
          -                    Subtract 2 by 1.
           /                   Divide 3 by the result of the previous.
            *                  Multiply 4 by the result of the previous.
             r                 REVERSE THE STACK!
              ***              Multiply 10, 9, 8, and 7.
                 v             Capture that number as a temporary variable.
                  {v}          Rotate the stack left, push the temp variable, rotate back.
                     /         Divide the 10*9*8*7 bit by 6.
                      v{v}/    Dividing the result of that by 5.
                           *   Multiply the remaining numbers.
                            N  Output the number on the stack (2016)
share|improve this answer
    
Ah, you maybe should add this in the explanation. – nicael Jan 6 at 19:42
    
To clarify, this would not be a valid entry as it does not contain 10 before 9 (a does not count). – rybo111 Jan 7 at 0:29

Milky Way 1.6.5, 28 25 bytes

10+9*(8)7;^*6*5/4*3/2*A!1

Explanation

10+9*                      ` perform 10*9 (leaves 90 at TOS)
     (8)7;^*               ` get rid of 8 and multiply the TOS by 7
            6*5/4*3/2*A    ` perform TOS*6/5*4/3*2 (leaves 2016 at TOS)
                       !   ` output the TOS
                        1  ` push 1 to the stack
share|improve this answer

BotEngine, 42 39 36 13x2=26

v109876543210
>ee   e@  eRP
share|improve this answer

><> (fish), 18 bytes

10987**r65r4*n;321

explaination:

multiplies 9 8 and 7 together to get 504, reverses the stack and reverses it again right before the 4 is added, then multiplies 504 and 4 to get 2016. Then prints the number and ends the program before the last 3 numbers (i could do it after too with no difference, if that matters rules-wise).

share|improve this answer

Python 2, 20 bytes

print 109876/54+3-21

Again, that same boring 2016.(740). Makes use of the fact that if you don't have a decimal number in your expression it returns an integer.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.