« Code Kata--How It Started | Main | Kata, Kumite, Koan, and Dreyfus »
Some sequences just keep turning up.
Think of binary numbers: sequences of 0's and 1's. How many n-digit
binary numbers are there that don't have two adjacent 1 bits? For
example, for three-digit numbers, five of the possible eight
combinations meet the criteria:
000,
001,
010,
011,
100,
101,
110,
111. What is the number for sequences of length 4, 5, 10, n?
Having worked out the pattern, there's a second part to the question: can you prove why that relationship exists?
Posted at 08:01 AM | Permalink
TrackBack URL for this entry:
www.typepad.com/services/trackback/6a00d83451c41c69e200d835152baf69e2
Listed below are links to weblogs that reference Code Kata Fifteen -- A Diversion:
» Solution to Code KataFifteen from Mousebender
Just felt like doing some programming exercise. My bookmarks led me to the code kata 15. First, the problem:
Think of binary numbers: sequences of 0s and 1s. How many n-digit binary numbers are there that dont have two adjacent ... [Read More]
Tracked on November 05, 2007 at 11:47 AM
The comments to this entry are closed.
f(n) = (ways to start number with 0) + (ways to start number with 1)
If the number starts with 0, the problem reduces to the n - 1 case. If the number starts with 1, the next bit must be 0, and we reduce to the n - 2 case.
So, for n > 2, f(n) = f(n - 1) + f(n - 2).
Posted by: Paul | July 08, 2008 at 11:18 AM
If you forbid m adjacent 1-bits, then the permitted n-bit numbers are those that begin with 0,1,...,m-1 adjacent 1s followed by a 0. Hence f(n) = f(n-1) + ... + f(n-m). (That holds for n>=m. For smaller n, you can either say directly that the number is 2^n or else set f(0)=1 and f(anything negative)=0 and use the recurrence for 1 onwards.)
Next question: fix m; then, when n is Very Large, roughly how big is f(n)? There's some nice mathematics behind this...
Posted by: g | February 09, 2007 at 05:37 PM
Peter's correct. The answer is f(n+2) where f(n) is the Fibonacci sequence. (i.e. f(1)=1, f(2)=1, f(3)=2, f(4)=3, f(5)=5 etc..).
For 3 bits: f(3+2) = 5.
For 4 bits: f(4+2) = 8.
etc...
Now the question is, what's the answer for this puzzle if we change it to 3 adjacent bits? 4 adjacent bits? m adjacent bits?
Posted by: Spikey | February 06, 2007 at 11:23 PM
It's a Fibonacci sequence. 1 bit = 2, 2 bits = 3, 3 bits = 5, etc.
If you look at have already calculated the nth # of bits, then n+2 is four groups of the same numbers, each preceded by 00, 01, 10, 11.
The first one is the same as n, the second one will eliminate all numbers that have a 1 in the high order bit. These first two groups, when combined, is the solution to n+1.
The third group gives the same result as n, and in the fourth group, no numbers qualify (11 doesn't qualify by definition). So the third+fourth group = n.
So:
f(n+2) = 1st+2nd+3rd+4th blocks
f(n+2) = (1st+2nd) + (3rd+4th)
f(n+2) = f(n+1) + f(n)
f(n+2) = f(n) + f(n+1)
Which is the definition of the Fibonacci sequence. Sorry about the sloppy mathematical notation.
-Peter
Posted by: Peter Christensen | February 06, 2007 at 11:27 AM
'fraid not...
Posted by: Dave Thomas | February 01, 2007 at 05:52 PM
2^(n-1) + 1
Posted by: Jeff | February 01, 2007 at 05:40 PM