Skip to content

Instantly share code, notes, and snippets.

@lasarus
Created August 8, 2013 19:33
Show Gist options
  • Save lasarus/6187910 to your computer and use it in GitHub Desktop.
Save lasarus/6187910 to your computer and use it in GitHub Desktop.
Brainfuck code to calculate the largest number with n digits divisible by m
;; solution to problem in pseudo code ;;
; maxval:=10^a-1
; minval:=10^(a-1)
; result:=maxval-(maxval%b)
; read n from input:
; cell layout (every cell is surrounded by brackets): [n][tmp][char]
>>, ; goto char and take input from stdin
----- ----- ----- ----- ----- ----- -- ; if character is 32 (' '), stop loop before it has begun
[+++++ +++++ +++++ +++++ +++++ +++++ ++ ; reset character
<< ; go to n
[->+<]>[-<+++++ +++++>] ; move n to tmp and then back but instead of adding one at a time, add 10.
; this functions as multiplying n by 10
>----- ----- ----- ----- ----- ----- ----- ----- ----- --- ; move to char and remove 48 ('0')
[-<<+>>] ; add char to n
,----- ----- ----- ----- ----- ----- --] ; take input and subtract 32 (' ') if the character aldready is 32, end loop
; read m:
; cell layout: [n][m][tmp][char]
; this is almost the same thing as before but instead of subtracting 32 to end loop on space,
; we will be subtracting by 10 to end on newline
>, ; move to char and take input
----- ----- ; subtract by 10 to end on newline
[+++++ +++++ ; reset char
<< ; move to m
[->+<]>[-<+++++ +++++>] ; move m to tmp and back but 10 at a time
>----- ----- ----- ----- ----- ----- ----- ----- ----- --- ; remove 48 from char
; '0' in ascii is 48
; '9' = 48 + 9
; '9' - 48 = 9
[-<<+>>] ; add char to m
,----- ----- ; subtract 10 from m to end loop if it's a newline
]
; calculate maxval
; equation: 10^n-1
; cell layout: [n][m][maxval][tmp][mult]
; mult will be set to n, and then subtracted every multiplication
<<< ; move to n
[->>>+>+<<<<]>>>[-<<<+>>>]< ; move n to both tmp and mult, and then move back tmp to n (copy n to
; mult and use tmp as temporary cell
+++++ +++++ ; add 10 to maxval
>> ; move to mult to prepare for loop
-[ ; subtract to make it a while instead of do while loop
<<[->+<]>[-<+++++ +++++>]> ; go to maxval and move it to tmp, then move tmp back to maxval
; but instead of adding 1 everytime, add 10 (multiply by 10)
-] ; subtract 1 from loop and end on 0
<<- ; remove one from maxval (last step)
; calculate minval:
; I actually didn't use this but I planned to and didn't want to remove it
; [n][m][maxval][minval][tmp][mult]
; equation; 10^(n - 1)
<< ; go to n
[->>>>+>+<<<<<]>>>>[-<<<<+>>>>]< ; move n to tmp and mult and then move tmp back to n
+++++ +++++ ; add 10 to minval
>> ; move to mult to prepare for loop
--[ ; subtract 1 to make it a while loop, and 1 more to do the (n-1) part of the equation
<<[->+<]>[-<+++++ +++++>]> ; move to minval and multiply it by 10 using tmp as temporary cell
-] ; subtract 1 from loop and end on 0
<< ; move to minval
; move everything into place for last calculation
; cell layout: [n][m][maxval][minval][maxval tmp1][maxval tmp2][tmp1][tmp2][tmp...]...
<[->>+>+>+<<<<]>>>>[-<<<<+>>>>]< ; copy maxval to the maxval tmp cells and use tmp1 as temporary cell
<<<<[->>>>>+>+<<<<<<]>>>>>>[-<<<<<<+>>>>>>]<< copy m to tmp1 using tmp2 as temporary cell
; calculate divmod
; cell layout: [n][m][maxval][minval][result (currently maxval)][maxval tmp][m tmp]
[->-[>+>>]>[+[-<+>]>+>>]<<<<<] ; magic divmod algorithm
; it works by taking the cell you are pointing to and dividing it by
; the cell after it.
; The output is formatted like this:
; [0][1-n%d][n%d][n/d]
; cell layout: [n][m][maxval][minval][result (currently maxval)][0][1-maxval%m][maxval%m][maxval/m]
; we are only interested in maxval%d
; we are currently pointing at the [0]
>>[-<<<->>>] ; subtract result cell by maxval%m (this produces the result for the whole program)
<[-]>>[-]<< ; remove the unecessary cells ([1-maxval%m][maxval/m])
; cell layout: [n][m][maxval][minval][result][][X]
; this algorithm prints result, but it starts 2 steps infront of it (X)
++++++++++<<[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]>>[-]>>>++++++++++<[->-[>+>>]>[+[-
<+>]>+>>]<<<<<]>[-]>>[>++++++[-<++++++++>]<.<<+>+>[-]]<[<[->-<]++++++[->++++++++
<]>.[-]]<<++++++[-<++++++++>]<.[-]<<[-<+>] ; a compact print algorithm I found on google
[-]+++++ +++++. ; set the current cell to 0, add 10 and print it (newline)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment