Hey everyone.
The recent code golf contest set by LogicalJoe for the smallest BF interpreter in TI-Basic (good job lirtosiast, as always) got me into exploring BF more deeply and attempting to write a few scripts. I've compiled what I've worked on below and will continue to update this thread with more (and feel free to add your own). These are all personal derivations and definitely not the most efficient out there, but they get the job done.
If somebody wants to try executing these using one of the Basic interpreters I'd be interested to get a sense of the speed; for a quicker runtime I'd recommend: https://fatiherikli.github.io/brainfuck-visualizer/.
Fibonacci (in place)
Raw: .>+.[<[->>+<<]>[-<+>>+<]>[-<+>]<.]
Code:
Multiply A & B
Raw: ,>,[<[->>+>+<<<]>>>[-<<<+>>>]<<->>>+<<<]>>>[-<<<+>>>]<<.
Code:
Logical A<=B
Raw: >,>,<[-[>]<[-[<]]>]>
Code:
A modulo B
Raw: >>>>>,>,<<<<+[>>>[-<<<<<+>>>>>]>[-<<<<<+>>>>>]<<<<[-]<<[->>+>>>+<<<<<]>[->->>+>->+<<<<<]>>>[-<<<+>>>]<<<[-[>]<[-[<]]>]>]>.
Code:
Prime Generator
Raw: ++>+++[[->+>+<<]>>++<[-<+>]>[>>+>>>>>+<<<<<[[-]>>>>[-]<<<<<<[->>>>>+>+<<<<<<]>>>>>>>++<<[-<<<<<+>>>>>]<<+[>>>[-<<<<<+>>>>>]>[-<<<<<+>>>>>]<<<<[-]<<[->>+>>>+<<<<<]>[->->>+>->+<<<<<]>>>[-<<<+>>>]<<<[-[>]<[-[<]]>]>]>[<<]<<]<<[->>>>>>+<<<<<<<+>]>>>>>>>[-<->]<[<<<<<<<[->+<]>++>>>>>>[-]]<<<<<<]<.]
Code:
The prime generator is terribly slow; I'm looking for a way to check only previous primes as potential divisors rather than every odd number less than the candidate, though it will be tricky. Have fun with these for the moment.
The recent code golf contest set by LogicalJoe for the smallest BF interpreter in TI-Basic (good job lirtosiast, as always) got me into exploring BF more deeply and attempting to write a few scripts. I've compiled what I've worked on below and will continue to update this thread with more (and feel free to add your own). These are all personal derivations and definitely not the most efficient out there, but they get the job done.
If somebody wants to try executing these using one of the Basic interpreters I'd be interested to get a sense of the speed; for a quicker runtime I'd recommend: https://fatiherikli.github.io/brainfuck-visualizer/.
Fibonacci (in place)
Raw: .>+.[<[->>+<<]>[-<+>>+<]>[-<+>]<.]
Code:
.>+. //Begin the list with 0,1
[
<[->>+<<] //Move F(n-2)
>[-<+>>+<] //Add F(n-1) to new F(n-2) and duplicate it
>[-<+>]<.] //Move duplicate F(n-1) to the (n-2) slot and display
]
Multiply A & B
Raw: ,>,[<[->>+>+<<<]>>>[-<<<+>>>]<<->>>+<<<]>>>[-<<<+>>>]<<.
Code:
,>, //Input A & B
[ //Loop to add A to running sum B times
<[->>+>+<<<] //Add A to running sum (initially 0) and create duplicate of A
>>>[-<<<+>>>] //Move duplicate A back to first cell
<<->>>+<<< //Subtract 1 from B counter and add 1 to duplicate counter
]
>>>[-<<<+>>>] //Move B to original position (mostly for style)
<<. //Display product
Logical A<=B
Raw: >,>,<[-[>]<[-[<]]>]>
Code:
>,>,< //Input A & B
[ //Loop to subtract 1 from A & B until one of them is 0
-[>] //Subtract 1 from A, move until reaching a 0 and terminate if B is 0
<[-[<]]> //Subtract 1 from B, move until reaching a 0 and terminate if A is 0
]>. //Return B-A+1 if A<=B, 0 otherwise
A modulo B
Raw: >>>>>,>,<<<<+[>>>[-<<<<<+>>>>>]>[-<<<<<+>>>>>]<<<<[-]<<[->>+>>>+<<<<<]>[->->>+>->+<<<<<]>>>[-<<<+>>>]<<<[-[>]<[-[<]]>]>]>.
Code:
>>>>>,>, //Input A & B (but shifted over a bit)
<<<<+ //Allow the program to enter the loop
[ //Loop to subtract B from A until the difference is less than B
>>>[-<<<<<+>>>>>] //Move A toward the front of the cells
>[-<<<<<+>>>>>] //Move B toward the front of the cells
<<<<[-]<< //Zero the "leftovers" from the previous iteration
[->>+>>>+<<<<<] //Move A & duplicate it
>[->->>+>->+<<<<<] //Move B & subtract it from A
>>>[-<<<+>>>] //Move A next to A-B for comparison
<<<[-[>]<[-[<]]>]> //Compute logical A<=B (see above); exit loop if A-B is less than B, or replace A by A-B and repeat
]>. //Display A mod B
Prime Generator
Raw: ++>+++[[->+>+<<]>>++<[-<+>]>[>>+>>>>>+<<<<<[[-]>>>>[-]<<<<<<[->>>>>+>+<<<<<<]>>>>>>>++<<[-<<<<<+>>>>>]<<+[>>>[-<<<<<+>>>>>]>[-<<<<<+>>>>>]<<<<[-]<<[->>+>>>+<<<<<]>[->->>+>->+<<<<<]>>>[-<<<+>>>]<<<[-[>]<[-[<]]>]>]>[<<]<<]<<[->>>>>>+<<<<<<<+>]>>>>>>>[-<->]<[<<<<<<<[->+<]>++>>>>>>[-]]<<<<<<]<.]
Code:
++>+++ //Initialize list with 2,3
[ //Loop to generate new candidate prime
[->+>+<<] //Move & duplicate newest prime (initialized with 3)
>>++< //Add 2 to newest prime to create candidate
[-<+>]> //Move duplicate back into prime list
[ //Loop to move to new candidate prime
>>+>>>>>+<<<<< //Initialize specific computation artifacts and test divisor
[ //Loop to test new candidate prime
[-]>>>>[-]<<<<<< //Zero the "leftovers" from modulo calculations
[->>>>>+>+<<<<<<] //Move & duplicate candidate prime
>>>>>>>++<< //Add 2 to potential divisor
[-<<<<<+>>>>>] //Move duplicate of candidate back to original cell
<<+
[ //Loop to compute candidate modulo divisor
>>>[-<<<<<+>>>>>]>[-<<<<<+>>>>>]<<<<[-]<<[->>+>>>+<<<<<]>
[->->>+>->+<<<<<]>>>[-<<<+>>>]<<<[-[>]<[-[<]]>]>
]>[<<]<< //Move data pointer if remainder is 0 or not; exit the loop if remainder is 0
]
<<[->>>>>>+<<<<<<<+>] //Move candidate prime into list
>>>>>>>[-<->] //Test if candidate prime = divisor; the candidate is prime if this is true
<[<<<<<<<[->+<]>++>>>>>>[-]] //Move candidate out of list if not prime and add 2
<<<<<<
]<. //Display newest list addition
]
The prime generator is terribly slow; I'm looking for a way to check only previous primes as potential divisors rather than every odd number less than the candidate, though it will be tricky. Have fun with these for the moment.