What is the shortest LOOP program that outputs 2016?

-3

1

LOOP

This challenge is based around a modified version of the programming language LOOP.

A program may be of any of the following forms:

  • P;Q, where P and Q are subprograms
    • Subprogram P is executed, then subprogram Q is executed.
  • x++, where x is a variable
    • Variable names are the letter x followed by a whole number (0, 1, 2, etc.). The value of variable x is incremented and assigned to variable x.
  • x:=y, where x and y are variables
    • The value of variable y is assigned to variable x
  • LOOP x DO P END, where x is a variable, and P is a subprogram
    • Subprogram P is executed a number of times equal to the value of x at the start of the loop. If the value of x changes during the loop, the number of iterations does not change.

Spaces and newlines may be inserted anywhere.

For this challenge, you may not use constants.

For example, the code
LOOP x1 DO x2 := x2 + 1; LOOP x2 DO x1++ END END
will set x1 to the x1th triangle number, by adding 1, then 2 etc.

Task

All variables are initialized to 0. The output of the program is the final value of x0, and the length of a program is the total count of increment/decrement/assignment/loop. The goal is to output 2016 with a program that is as short as possible.

Here is an interpreter for the modified language used in this challenge.

Lew Baxter

Posted 2016-01-11T18:53:34.370

Reputation: 15

Question was closed 2016-01-11T22:00:33.547

1The LOOP interpreter you linked to doesn't seem to support the ++ notation. Do you know of one that does? – PhiNotPi – 2016-01-11T21:17:08.660

@PhiNotPi I'll work on writing one. – KSFT – 2016-01-11T21:20:32.323

I'm not sure what happened to the comment I put here. It was this: "How is this closed with three downvotes and no comments? The challenge should be self-contained and have the LOOP specification, but I think it's very clear what the challenge is--write a LOOP program that sets a variable to 2016 in as few lines as you can." – KSFT – 2016-01-11T21:23:10.380

@KSFT I deleted the comment. The post was previously closed as unclear but was reopened, so the comment saying that it's not unclear was no longer relevant. – Alex A. – 2016-01-11T21:30:55.243

@AlexA. It's still relevant to the two people who have still downvoted it. – KSFT – 2016-01-11T21:31:28.120

@KSFT I downvoted it because 1.) it's language-specific, 2.) an interpreter that supports the necessary operations does not yet seem to exist, 3.) I find the task uninteresting. I would imagine that the one other downvoter (as of this writing) felt the same way. – Alex A. – 2016-01-11T21:32:35.007

I'm closing this question as off-topic because answers cannot be verified as correct without a working interpreter that supports all necessary operators. – Alex A. – 2016-01-11T22:00:33.547

@AlexA. I've attempted to write an interpreter, and I put a link to it in the question. Can we reopen it now? – KSFT – 2016-01-12T22:36:55.983

The interpreter doesn't continue after an END correctly. Also I have a 99 byte answer: x1++;x1++;x1++;x1++;LOOPx1DOLOOPx1DOx2++;x2++;LOOPx1DOx3++ENDENDEND;x3--;LOOPx2DOLOOPx3DOx0++ENDEND. – Neil – 2016-01-14T11:43:58.277

I think this is not off-topic, but unclear. Nowhere in the description says that <var> := <var> + <var> syntax is allowed, but the example program includes it. – user202729 – 2018-07-19T03:12:18.720

Answers

1

Naive attempt, 15 statements

This uses the factorization of 2016 into 2^5*3^2*7.

x1++;
x1++;
x2:=x1;
x2++;
x3:=x2+x2;
x3++;
LOOP x1 DO
LOOP x1 DO
LOOP x1 DO
LOOP x1 DO
LOOP x1 DO
LOOP x2 DO
LOOP x2 DO
LOOP x3 DO
x0++
END
END
END
END
END
END
END
END

I've gotten this to run on the provided interpreter by replacing things like x1++; with x1:=x1+1;, although I did not use "strict mode."

PhiNotPi

Posted 2016-01-11T18:53:34.370

Reputation: 26 739

The linked interpreter doesn't seem to like x3:=x2+x2; otherwise I would suggest using x2:=x3+x1; to save you from having to loop twice over x2. – Neil – 2016-01-14T11:30:48.360