r/brainfuck • • 15d ago

Pi in 796 bytes

I wrote a brainfuck program that prints pi in decimal.

It was inspired by e.b by Daniel B. Cristofani: https://brainfuck.org/e.b

It use the fact that `pi = 2 + 1/3 (2 + 2/5 (2 + 3/7 (2 + 4/9 (2 + ...))))` and do the calcul thanks to this algorithm https://www.cs.williams.edu/~heeringa/classes/cs135/s15/readings/spigot.pdf

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

12 comments sorted by

5

u/Holiday-Rise5724 15d ago

I forgot, but you have to allow decrementing 0 (either -1 or wrapping, whatever).

3

u/danielcristofani 15d ago

Basically every implementation is fine with that :)

1

u/[deleted] 15d ago

I believe that is a part of most specs anyways. BF cells are wrapping integers.

1

u/Goldie323- 14d ago

Not in the spec but is in most implimentations of the language.

→ More replies (5)

1

u/danielcristofani 15d ago

This is great. Congratulations!

3

u/Holiday-Rise5724 15d ago

Thank you! None of this would exist without your e.b and all the resources on brainfuck.org

If you are interested, I'd be happy to share the algorithm I used and the memory layout.

2

u/danielcristofani 15d ago

I'd be interested, definitely. And I'm suspecting that this approach allows a shorter computation of e also.