2

So I'm trying to get the cells to hold such that the first cell holds null (0), the second holds 9*4, the second, 9*10, the third 9*12, the 4th, 9*13, and so on until the last cell in use has 9*15. That is, the following code so far in brainfuck:

+++++++++[>++++>++++++++++>++++++++++++>+++++++++++++>++++++++++++++>+++++++++++++++<<<<<<-]

Is there anyway to condense this code? That is, have the proper cells hold the numbers in question but use less characters to do it? I thought of using nested loops but I'm not exactly sure how to construct such a thing. I'm new to the language and I'm trying to test it out and I think I'm overthinking the problem.

Zoe
  • 27,060
  • 21
  • 118
  • 148
flaw600
  • 161
  • 1
  • 2
  • 8

1 Answers1

3

To use inner loops find out factors of the number you want to put in a cell.

Suppose you want to set a cell to 48

You can make the outer loop loop twice. So far so good, you use a temporary cell to hold the loop value, and increment the target cell by 24 inside so you end up with 48.

Now suppose I want to break down the body of the loop, which is currently adding 24 on every iteration. You do the same process. Find out factors of 24: for this example let's choose 6 and 4. So in the inner loop you put in a 2nd loop (which uses a 2nd temporary cell to hold the inner loop's iteration count), loop 4 times adding 6 each time. Every time this inner loop runs, the target cell ends up with 24 added to it, and the inner loop runs twice (the outer loop loops twice) so the target cell ends up with 48.

Here is the example using just 1 temporary cell

++[>++++++++++++++++++++++++<-]

This uses cell 0 as the counter and sets cell 1 to 48 by adding 24 twice.

And here is the 2nd example with 3 factors: 2, 4, 6 (2*4*6==48)

++[>++++[>++++++<-]<-]

This uses cells 0 and 1 as temporary cells, and sets the target cell (cell 2) to 48 as well. As you can see, the content of the inner loop (++++[>++++++<-]) is just a normal loop like in the 1st example.

It is clearly obvious that the 2nd one is shorter, but it might run very slightly slower (not that this is a real concern... you're using BF in the first place, you're not looking for performance)

Now since you want to set multiple cells at once, it's pretty easy to apply the above to your code. You find out the factors in the same way as I described above, and put the factor that's common to all of them as the outer loop's counter, and then you construct individual inner loops for each cell inside it using their remaining factors. You could even merge some of them if any of the inner ones share factors as well.

Another trick which could shorten your code when the number isn't as cleanly divisible, is to get as close as you can to the number you want and adjust it at the end.

To use the same example I used above, If I wanted to set the cell to 49 instead of 48, I'd use the same code that sets it to 48, and then add 1 at the end (or set it to 50 and subtract 1) and the resulting code might still be shorter.

Cedric Mamo
  • 1,724
  • 2
  • 18
  • 33
  • Could you give an example as to how to apply it to setting multiple cells at once? Not necessarily my problem, but something I can better apply to mine? Thanks! – flaw600 Dec 05 '15 at 18:02
  • in the 1st example, I was adding 24 inside the loop. I literally replaced that string of 24 + symbols with another loop designed to add 24, while keeping the rest of the code the same. In your case, you have multiple different sequences of + symbols in the loop (one for each cell you're setting). Just treat them individually, and replace each of them with a loop in the same way (finding out a pair of factors for each and making a loop with those). Keep in mind that sometimes replacing them with a loop will make the code longer. – Cedric Mamo Dec 05 '15 at 18:11
  • Also since the inner loop would use a temporary variable, which ends up as 0 at the end of the loop, you'll still only need 2 temporary cells for the entire loop; one for the outer loop, and another that gets reused for every inner loop. – Cedric Mamo Dec 05 '15 at 18:12
  • Since the latter sections of my cells (9*12...9*15) are just adding plus one to being multiple by 9, would there be an easy way to just tack on that additional multiplication to those cells specifically? And thanks for your help! When I get to a place where I can try out your suggestions, I'll accept your answer :) – flaw600 Dec 05 '15 at 18:20
  • I'm not sure. There might be but if there is it eludes me right now. You'll just have to be creative. BF is a simple language, so you often have to do things the simple way. You might save some code if, for example, two cells have two factors in common, which allows you to merge the inner loop for them as well. Stuff like that. But again, it might not be worth it. The more cells you try to set with one loop, the further away the temporary cell is going to be for each one, so you'll end up adding more < and > symbols. – Cedric Mamo Dec 05 '15 at 18:24
  • I actually tried to shorten the code you posted. Since inside the loop you're only adding small numbers (highest being 15 for the rightmost cell) then none of the inner ones would benefit from being replaced by a loop. If adding a number smaller than 15 (all your cells except for the last one) then a simple sequence of + is the shortest possible code (I wrote out a program to generate these kinds of loops). – Cedric Mamo Dec 05 '15 at 23:22
  • For adding 15 the code +++[>+++++<-] will work and is only 13 characters long so it's technically saved code. However in your case you'd need to add an extra > and < characters to put the temp cell to the right of it, so that the final cells end up next to each other. So you'd need to replace the final +++++++++++++++ with >+++[<+++++>-]< which is 15 characters long anyway. So no savings for you there. The code you posted is already as short as it can be. – Cedric Mamo Dec 05 '15 at 23:24