Last active
August 26, 2019 00:04
-
-
Save melwyn95/162da75c8e9b438df8ecb2a731f87a04 to your computer and use it in GitHub Desktop.
fibo.bf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Program to find the nth term of fibonacci sequence where n is input between 2 and 9 (inclusive) | |
, input (number between 2 and 9) | |
>>++++++[-<++++++++>]<[-<->]< subtract 48 from the 1st cell (48 is the ASCII value of zero | |
we subtract inorder to convert from ASCII to integer) | |
-- subtract 2 from the 1st cell since first 2 terms of the | |
fibonacci series is 1 and 1 | |
>+>+ initialize the 2nd and the 3rd cells to 1 (the first 2 terms | |
of the fibonacci series) | |
<< goto 1st cell | |
[ loop: | |
>[->>+<<] copy the 2nd cell to the the 4th cell | |
> goto 3rd cell | |
[->+>+<<] add the value in the 3rd cell to the 4th cell and also | |
copy the value of the 3rd cell the the 5th cell | |
>> goto 5th cell | |
[-<<<+>>>] copy the value of the 5th cell to the 2nd cell | |
< goto 4th cell | |
[-<+>] copy the value of 4th cell to the 3rd cell | |
<<< goto 1st cell | |
- subtract the value in the 1st cell by 1 and goto next | |
iteration of the loop | |
] end of the loop | |
>> goto the 3rd cell the 3rd cell contains the answer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment