how to find out the solution to this problem in python/java or any other language:
Thanks in advance
Since a program isn't a proof and you would still need to prove it, here is some Python code:
def zig_zag(seq):
"""tests if binary sequence seq satsifies zig-zag pattern"""
for i in range(len(seq)-1):
if (i%2 == 0 and seq[i] > seq[i+1]) or (i%2 == 1 and seq[i] < seq[i+1]):
return False
return True
def count_zig_zags(n):
"""counts the number of binary zig-zag patterns of length n"""
count = 0
for i in range(2**n):
b = bin(i)[2:]
if zig_zag(b): count += 1
return count
For example:
>>> [count_zig_zags(n) for n in range(1,12)]
[2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]
A proof would be via strong induction.
000, 010, 011, 110, 111.n can be extended to a valid solution of length n+1 in either 1 way or 2 ways, and that the number that can be extended in 2 ways is the same as the number of valid sequences of length n-1 (proof, which seems to require 2 cases depending on whether n is even or odd, not given).
n. For reasons that I can't quite articulate, I suspect that the answer has something to do with Fibonacci numbers.