Recursive function to compute the number of sequences of n binary digits.

How to Write a recursive function to compute the number of sequences of n binary digits that do not contain two 1s in a row. Write a main function to test this function.

Input:Input consists of the number of binary digits in the sequence.

Output:Output consists of the number of sequences of given binary digits that do not contain two 1s in a row

Sample Input:
Enter the number of binary digits in the sequence:
5
Sample Output:
Number of sequences of 5 binary digits that do not contain two 1s in a row is:
13

Questions by souji1425

Showing Answers 1 - 3 of 3 Answers

Santanu Bhattacharya

  • Feb 28th, 2016
 

Code
  1.  def binary_rec(n)

  2.     if n == 1

  3.       return 1

  4.     elsif n == 2

  5.       return 3

  6.     else

  7.       binary_rec(n-1) + binary_rec(n-2)

  8.     end

  9.   end

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions