LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 April 10 2016

ranibalaa
Member

Write a brainfuck program

Hey guys,
Ive been trying to write a brainfuck program but completely stuck any help would be appreciated;
it should take an integer n and return 3n^2 -2n +9 and print the results.
Any help would be greatly appreciated
thanks anyways

Offline

#2 April 10 2016

NuclearVision
Member

Re: Write a brainfuck program

You got me busy...
I'm not sure if this is perfect.

,[-<++>>+>+]>[->[->+<<<+][>+>-]]<<[->+++]>[-<+]<<[->-]>+++++++++

Last edited by NuclearVision (April 11 2016)

Offline

#3 April 11 2016

rolf
Member

Re: Write a brainfuck program

I'm trying myself and brainfuck and doing a multiplication is a puzzle, unresolved so far!

Offline

#4 April 11 2016

NuclearVision
Member

Re: Write a brainfuck program

Rolf, check this out.
Doing multiplication is explained in the topic.

But the special thing about this one, Not knowing the integers to be multiplied which is challenging.
I'll explain my code tonight.

Offline

#5 April 11 2016

Johnaudi
Member

Re: Write a brainfuck program

I coded this yesterday for AUB rally paper 204. I'll ask if I can get my code back in a bit.
Note: they want to input a number from 0 to 999, not a single integer.

Last edited by Johnaudi (April 11 2016)

Online

#6 April 11 2016

NuclearVision
Member

Re: Write a brainfuck program

Any integral number works i'm not sure what you mean with single integer.
Oh, and i find it impractical to use brainfuck in rally paper (assuming there are geeks... I mean not everyone has access to such knowledge) or timed competitons... Who'll decide if it's correct or not, it takes time to understand such implementations, and to explain it, unless it's not verified.

Offline

#7 April 11 2016

Joe
Member

Re: Write a brainfuck program

I still struggle to manipulate numbers higher than 255. However assuming very low numbers (like 1 and 2), this is actually a good exercise. I think Johnaudi meant single digit.

@NuclearVision: I don't think your code works. Maybe you miscopied part of it? Why would you start going to the left when you're in cell0?

Offline

#8 April 11 2016

NuclearVision
Member

Re: Write a brainfuck program

going left will put me at cell30000 which is the last if i'm not mistaken, so why go deep right.

,[-<++>>+>+] i start by emptying cell 1, moving n to cells 2 and 3, at cell 30000 i'll have 2n to be used later for substraction.
>[->[->+<<<+][>+>-]] now this is tricky, i move to cell2 which has n right now, start decrementing one point each time while also copying everything from byte 3 to 1, then build n again at byte 3. So if n=4, the following will happen 4 times, substract 1 point from byte 2, add 4 points to cell1, while preserving the multiplier which is 4 at byte 3 everytime the decremention finishes. When 4 subsctractions are made at cell2, cell 1 will have 4*4 or 4^2, cell2 will have nothing left.

<<[->+++]>[-<+]<<[->-]>+++++++++ Now this takes us back to cell1 which has n^2. substract everypoint and add three to cell2 which was just emptied, then move everything from cell2 to cell1 now we have 3n^2 at cell1, now go to cell30000 keep decr until it's empty while also decrementing cell1. we know that's not gonna cause problems 3n^2 is greater than 2n. Finally add 9 to cell1, which will have your number.

Last edited by NuclearVision (April 11 2016)

Offline

#9 April 11 2016

Joe
Member

Re: Write a brainfuck program

,[-<++>>+>+] i start by emptying cell 1, moving n to cells 2 and 3, at cell 30000 i'll have 2n to be used later for substraction

Not exactly. You need to go back to cell 1 before closing the bracket.

if you want to have:

  • 0 on cell1

  • n on cell 2 and 3

  • 2n on cell 30000

You probably want something like this:

[
  -
  <++
  >>+
  >+
  <<  # that's the part you're missing.
]

The rest of the reasoning looks legit up front but I have to read it closer.

Offline

#10 April 11 2016

NuclearVision
Member

Re: Write a brainfuck program

I edited my code.
I thought when one incrementation ends, the pointer gets back to original cell automatically, isn't that right?

If not, it should look like this

,[-<++>>+>+<<]>[->[->+<<<+>>][+>-<]<]<<[->+++<]>[-<+>]<<[->-<]>+++++++++

Last edited by NuclearVision (April 11 2016)

Offline

#11 April 11 2016

Johnaudi
Member

Re: Write a brainfuck program

Joe wrote:

I still struggle to manipulate numbers higher than 255. However assuming very low numbers (like 1 and 2), this is actually a good exercise. I think Johnaudi meant single digit.

@NuclearVision: I don't think your code works. Maybe you miscopied part of it? Why would you start going to the left when you're in cell0?

There are two solutions for removing the limit from 255. You can either have a something like:
[0],[1],[2]
0,3,42

[0] is pointer 0, and has the value of 0.
In [1] you have the value 3, which is in definition, 3 times 255, and in [2] you have 42, so in total the number is: 3*255 + 42 = 802.
Once [1] reaches the limit (255++ == 0) then [0] will have a value added to it.

The other (easier) solution, most compilers (such as http://copy.sh/brainfuck ) have an option to where you can adjust the cell size from 8 Bits (255) to 32 bits.

Last edited by Johnaudi (April 11 2016)

Online

#12 April 12 2016

Joe
Member

Re: Write a brainfuck program

@Johnaudi, yes you need to set some sort of integer system based on fixed cell sizes. But that doesn't even come close to solving the issue of input. "," by definition accepts one byte of input, which makes it difficult to enter any larger number. I'm still unclear how to deal with this.

Changing the cell size to 4bytes sounds like a good solution to the problem, but not being standard brainfuck it won't run on standard compilers.

Offline

#13 April 12 2016

NuclearVision
Member

Re: Write a brainfuck program

is , subject to loop? was thinking of something like [,] until empty.

Offline

#14 April 12 2016

Johnaudi
Member

Re: Write a brainfuck program

There's the code answering your question: (3n^2 -2n +9)

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

Note: You must use a 32Bit cell size for it to work for numbers bigger than 9.

Last edited by Johnaudi (April 12 2016)

Online

#15 April 13 2016

Joe
Member

Re: Write a brainfuck program

NuclearVision wrote:

is , subject to loop? was thinking of something like [,] until empty.

the definition of ',' is to read a single byte from stdin and store it in the current cell.

I don't know how common implemetations behave, but the definition doesn't read the whole stdin.

Offline

#16 April 13 2016

Ra8
Member

Re: Write a brainfuck program

I was one of the problem setters in the AUB Rally Paper. I set this problem and corrected it. More than half the teams were able to do it correctly!
Well, to read the whole stdin, you should do something like: ,[>,] this will read all the characters in stdin until we read the \0 character which its value is 0 so the loop will terminate.

Offline

#17 April 13 2016

NuclearVision
Member

Re: Write a brainfuck program

do you mean ,[->+<,] but again how would the second cell fit the excess of 1 byte.

Offline

#18 April 13 2016

Joe
Member

Re: Write a brainfuck program

@Ra8 but then how will you now the size (in cell count) of your number?

Offline

#19 April 13 2016

Ra8
Member

Re: Write a brainfuck program

You don't need to know the size of the input per say:
If we do:

>,[>]

This is a simplified version:
Each digit will be in a cell, so for example reading the input 123, your memory will look like this: 0 49 50 51 0, and the tape will be pointing at  the 5th cell. All you have to do now is deduct 48 from each of the non zero cells to get the actual integer value.
After that you have to multiply and add the right most by 1, then by 1*10, then by 1*10*10 etc.. until you reach the 0.

After reaching 0 49 50 51 0, you can go back to the first cell (49) by doing <[<]>, you don't actually need to know how many cells are occupied.

Check @JohnAudi's code first line he did something similar

Offline

#20 April 13 2016

Johnaudi
Member

Re: Write a brainfuck program

@Ra8 if you've corrected my code, note that I had "Use 32 bits on copy.sh/brainfuck" commented - which is a stupid move from me because there's a "dot" in that comment.

Last edited by Johnaudi (April 13 2016)

Online

#21 April 13 2016

Ra8
Member

Re: Write a brainfuck program

Johnaudi wrote:

@Ra8 if you've corrected my code, note that I had "Use 32 bits on copy.sh/brainfuck" commented - which is a stupid move from me because there's a "dot" in that comment.

It's a bit too late, the results of the Rally were announced on Monday. But yeah, your team (204) was one of the teams who got it correct.

Offline

Board footer