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:
.>+.    //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.
You'd really well with all of that!
Here's a trick:
In loops, set the pointer's 'home' location to 255 (-1 in the golf code I did) and after the loop use +[-<+]- to get back home.

EDIT:
To only check prime numbers, you are making a list 2 3 5 7 etc. Place 2 holes in it like this: 255 2 0 0 3 5 255 0 0… to indicate your position. Copy the number to the end of the list something like this:
Code:
[<] //go to the hole
> // so pointer is not 0
[-<+>] // copy to the middle of the hole
< // pointer is not 0
[
 -<+> // copy to the left of the hole
 >+[->+]- // go to the end of the chain
 >+ // add one
 [<] // return to the hole
 < // position reset; if the number has been copied this will be 0
]
>>+[->+]- // go to the end of the list

You seem really good with BF, so I'll let you figure out how to reset it Smile

EDIT2: another method is to leave spaces between the numbers: 0 2 0 3 0 5 etc… and placing a 1 in the location to indicate which number you're on (I will not provide any code for this because it is incredibly easy).

Side note: don't use "," in your comments.

P.S. Thank you for competing in the BasicF Golfing Challenge!
I'm just gonna leave this link here...
Turns out it is a really bad idea to sit in front of a screen staring at BF code for 7 hours.
My brain got fried. 🤯
I did make this:
Code:
.+[.[>+>+<<-]>]
But that's all that ended up working after all that time. :/
I'm not going to be able to code for a while now.
How and what is BF code. I had the interpreter but I couldn't figure out how to use it.
Read the wiki: https://en.wikipedia.org/wiki/Brainf--k

EDIT: here's the video I learned from: https://www.youtube.com/watch?v=qK0vmuQib8Y
Oh thanks!
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement