Print 'A' 1000 times with BrainFuck

11

1

What is the best BrainFuck code (in terms of code size) to print 'A' 1000 times ?

My approach is:

  • set 'A' to p[0]
  • set 255 to p[1] to display 255 'A', 3 times
  • set 235 to p[1] to display 235 'A'

This is not effective but I cannot find a way to use tow counters simultaneously like a multiplication

Is there a better approach than a multiplication ?

A more general question: is there a rule to make a multiplication with a large number with the smallest possible code?

Nelson G.

Posted 2019-06-16T20:53:56.333

Reputation: 317

4This is a good place to start. Welcome to Code golf! :) – FryAmTheEggman – 2019-06-16T21:02:08.413

3

I think you want to just use a nested loop, but I don't know BF very well. Have you seen Brainfuck tips? Also probably the esolangs page on Brainfuck constants would be a useful resource here.

– Jonathan Allan – 2019-06-16T21:02:49.157

4I think you should clarify best BrainFuck code. Are you in search of most readable, most elegant, using the least amount of + characters or simply highest brevity? – Jonathan Frech – 2019-06-16T22:17:51.750

@Jonathan Allan: Yes, that's the purpose of this question : How to use a nested loop. It's a fascinating language close ASM but I don't understand some aspects – Nelson G. – 2019-06-17T06:31:22.703

Could I use this variant on BF -> https://github.com/gergoerdi/brainfuck64

– Shaun Bebbers – 2019-06-17T10:50:11.533

Question author had edited the question to clarify best means smallest code size. It about asking golf tips for a clear defined task with some certain language. I would consider it clear and on topic. So this question should be reopen imo. – tsh – 2019-06-19T07:12:04.833

It is tagged code-golf. It's pretty clear and this is on-topic. – mbomb007 – 2019-06-19T15:12:07.257

There is still some confusion on my part. Is the OP looking for the shortest code to generate 1000? Or, given any arbitrary n, generate the shortest BF code to produce it? – Giuseppe – 2019-06-19T17:35:19.410

Answers

17

The method you seem to currently be using is 39 bytes:

>>+++[<-[-<.>]>-]++++[<----->-]<-[-<.>] (not including getting the A) (Try It Online!)

(loop 3 times, each time set the counter to 255 and print that many times, then subtract 20, subtract 1, and print that many times)

However, it is much shorter to loop 250 times and print 4 times each time (thanks to jimmy23013 for optimizing this over my original loop-4 loop-250 print-1 solution):

>------[<....>-] (16 bytes)

If your cells are unbounded (I'm assuming they're 8-bit otherwise you probably wouldn't try using 255 for golfing):

>>++++++++++[<++++++++++[<..........>-]>-] (42 bytes).

HyperNeutrino

Posted 2019-06-16T20:53:56.333

Reputation: 26 575

this seems to assume 8-bit cells, though... – John Dvorak – 2019-06-16T21:22:09.387

2@JohnDvorak: The question mentioned setting cells to 255 as a part of the most effective solution the OP could think of. That seems like a pretty clear indication of (ab)using 8-bit cell wrapping. – randomdude999 – 2019-06-16T21:25:35.523

@JohnDvorak What randomdude999 said, but I did add a method using 10x10x10 in case the cells are unbounded. – HyperNeutrino – 2019-06-16T21:37:20.887

250 times .... would be shorter. – jimmy23013 – 2019-06-17T09:36:17.070

@jimmy23013 ... not sure how I didn't think of that an still optimized my 10x10x10 solution to do that LOL. thanks! – HyperNeutrino – 2019-06-17T13:35:11.910

1

47 Bytes ( No Underflows )

I just did this solution in 47 bytes. I tried doing it in a different manner than I normally would trying to save space by juggling counters between two values. It assumes that A is preloaded into p[4].

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

Explained

Place 5 into p[0]
+++++
Loop 5 times to put p[1]=20, p[2]=25
[>++++>+++++<<-]>
Loop 20 times
[>
Move pointer to p[2] and loop 25 times.
Also move p[2] to p[3]

[
Increment p[3] from 0, effectively moving the value from p[2] to p[3]
>+
Print p[4]
>.
Decrement p[2]
<<-]
Shift over to p[3]
>
[
Decrement p[3]
->
Print p[4]
.<
Increment p[2]
<+>]
Move back to p[1] and decrement
<<-
Repeat until p[1] is 0
]

Josh Bacon

Posted 2019-06-16T20:53:56.333

Reputation: 11

1

the shortest way to get the number 65 for 'A' is >+[+[<]>>+<+]>, and then you just add HyperNeutrino's >------[<....>-] to the end of that. so the full code becomes >+[+[<]>>+<+]>>------[<....>-] (30 bytes)

Sagittarius

Posted 2019-06-16T20:53:56.333

Reputation: 169

How do you know that way is shortest? It certainly is shorter but do you know for certain that noone will find a shorter one? – Post Rock Garf Hunter – 2019-08-11T12:21:59.787

1

28: >+[+[<]>>+<+]------[>....<-]

– jimmy23013 – 2019-08-11T14:16:32.677

@SriotchilismO'Zaic yeah I didn't really mean it was the shortest lol – Sagittarius – 2019-08-30T20:56:55.823