Last active
February 28, 2016 14:21
-
-
Save santanub/a4ab38abbccb0bd87f2d to your computer and use it in GitHub Desktop.
6. Write a recursive function to compute the number of sequences of n binary digits that do not contain two 1s in a row. (Hint: compute how many such sequences exists that start with 0, and how many exists that start with a 1)
This file contains hidden or 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
| ## Problem no 6 recursion | |
| class Algo | |
| def count_binary_count_rec(n) | |
| if n == 1 | |
| return 2 ** n | |
| elsif n.zero? | |
| return 1 | |
| else | |
| count_binary_count_rec(n-1) + count_binary_count_rec(n-2) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment