User:None1/BF

From Esolang
Jump to navigation Jump to search

Some brainfuck programs I wrote (this is a copy of my brainfuck programs at GitHub).

Kolakoski sequence

This outputs the Kolakoski sequence, as the sequence is infinitely long, the program never terminates.

+++++++[>+++++++<-]>.+..-..[-] ; Print out "12211"
+>>>>++>>>>++>>>>+>+>>>+>>+<<<<<<<<<<<<<<<<<<< ; Store 1 2 2 1 1
                                               ;            s^ f^
+[ ; Infinite loop
    >>-[+>>>>-]+ ; Go to the slow pointer
    <[
        >>-[+>>>>-]+ ; Go to the fast pointer
        >>[>>>>]+++ ; Set top value to 3
        <<-[+<<<<-]+ ; Go back to the fast pointer
        <<[>>>+>[>>>>]<<<<-<<-[+<<<<-]+<<-] ; Set top value to 3 minus fast pointer value
        >>>[<<<+>>>-] ; Restore fast pointer value
        <<<[>>>>]<<<++++++[<++++++++>-]<. ; Output top value as character
        >++++++[<-------->-] ; Restore to number
        -[+<<<<-]+<- ; Decrement slow pointer value
    ]
    >>>[<<<+>>>-] ; Restore slow pointer value
    <-[+>>>>-]+ ; Go to the fast pointer
    [>>[>>>>]<<+<<<<-[+<<<<-]] ; Move fast pointer to top
    <-[+<<<<-]+[>>>>+<<<<-] ; Move slow pointer to the next
    >>-[+<<<<-]+ ; Move back to infinite loop flag
]

The minimized version is like this:

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

Random number generator

An RNG that uses this generator: f[i]=(f[i-1]+f[i-2]+f[i-3])%251, f[-2]=f[-1]=f[0]=1. Its period is 63253. Generates characters from 0 to 250.

[ Simple RNG that uses this generator: f[i]=(f[i-1]+f[i-2]+f[i-3])%251, f[-2]=f[-1]=f[0]=1. Its period is 63253. Generates characters from 0 to 250.]
+>>+>>+>+ ; Initialize the first three terms and the infinite loop flag
[ ; Infinite loop
    >>>++++++++++++++[<++++++++++++++++++>-]<-<< ; Initialize 251 (the divisor)
    -<[>>+[>->+<[>]>[<+>-]<<[<]>-]>>[<+<+>>-]>+<<<<<-]>>>>>[<<<<<+>>>>>-] ; Add the previous term using modulus algorithm
    <<<<<
    <<[>>>>+[>->+<[>]>[<+>-]<<[<]>-]>>[<+<+>>-]>+<<<<<<<-]>>>>>>>[<<<<<<<+>>>>>>>-] ; Add the term before previous term using modulus algorithm
    <<<<<<<
    <<[>>>>>>+[>->+<[>]>[<+>-]<<[<]>-]>>[<+<+>>-]>+<<<<<<<<<-]>>>>>>>>>[<<<<<<<<<+>>>>>>>>>-] ; Add the term before the term before previous term using modulus algorithm
    <<<.>[-]+ ; Move to infinite loop flag
]

The minimized version is like this:

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