11
\$\begingroup\$

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?

\$\endgroup\$
  • 4
    \$\begingroup\$ This is a good place to start. Welcome to Code golf! :) \$\endgroup\$ – FryAmTheEggman Jun 16 at 21:02
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Jonathan Allan Jun 16 at 21:02
  • 4
    \$\begingroup\$ I 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? \$\endgroup\$ – Jonathan Frech Jun 16 at 22:17
  • \$\begingroup\$ @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 \$\endgroup\$ – Nelson G. Jun 17 at 6:31
  • \$\begingroup\$ Could I use this variant on BF -> github.com/gergoerdi/brainfuck64 \$\endgroup\$ – Shaun Bebbers Jun 17 at 10:50
17
\$\begingroup\$

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).

\$\endgroup\$
  • \$\begingroup\$ this seems to assume 8-bit cells, though... \$\endgroup\$ – John Dvorak Jun 16 at 21:22
  • 2
    \$\begingroup\$ @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. \$\endgroup\$ – randomdude999 Jun 16 at 21:25
  • \$\begingroup\$ @JohnDvorak What randomdude999 said, but I did add a method using 10x10x10 in case the cells are unbounded. \$\endgroup\$ – HyperNeutrino Jun 16 at 21:37
  • \$\begingroup\$ 250 times .... would be shorter. \$\endgroup\$ – jimmy23013 Jun 17 at 9:36
  • \$\begingroup\$ @jimmy23013 ... not sure how I didn't think of that an still optimized my 10x10x10 solution to do that LOL. thanks! \$\endgroup\$ – HyperNeutrino Jun 17 at 13:35
1
\$\begingroup\$

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)

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Wheat Wizard Aug 11 at 12:21
  • 1
    \$\begingroup\$ 28: >+[+[<]>>+<+]------[>....<-] \$\endgroup\$ – jimmy23013 Aug 11 at 14:16
  • \$\begingroup\$ @SriotchilismO'Zaic yeah I didn't really mean it was the shortest lol \$\endgroup\$ – Sagittarius Aug 30 at 20:56
0
\$\begingroup\$

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
]
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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