« Code Kata--How It Started | Main | Kata, Kumite, Koan, and Dreyfus »

January 30, 2007

Code Kata Fifteen -- A Diversion

Some sequences just keep turning up.

A Diversion

At my son's karate school, they spend most of their time doing various exercises, with the occasional round of sparring thrown in. But every now and then the teacher finds a way to break the routine by injecting some kind of game or surprise into the mix. This kata is one of those. It's nothing serious, and unlikely to have practical benefits (unless you're working in some fairly specialized areas).

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?


I'm trying to track down a definitive source for this relationship: if anyone has a reference I'd love to see it. Thanks...

Posted at 08:01 AM | Permalink

TrackBack

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

Comments

spacer

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

spacer

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

spacer

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

spacer

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

spacer

'fraid not...

Posted by: Dave Thomas | February 01, 2007 at 05:52 PM

spacer

2^(n-1) + 1

Posted by: Jeff | February 01, 2007 at 05:40 PM

The comments to this entry are closed.

Subscribe to this blog's feed
spacer

Other links

  • My Blog
    Dave's blog, which stuff on Ruby, Rails, general programming, careers, and other stuff.
  • The Pragmatic Programmers
    Where it all started! Have a look at the books we're publishing on all sorts of programming topics. If you liked the kata, you might also like The Best of Ruby Quiz.

Archives

  • January 2007

The Kata

  • 1. Supermarket Pricing
  • 2. Karate Chop
  • 3. How Big, How Fast?
  • 4. Data Munging
  • 5. Bloom Filters
  • 6. Anagrams
  • 7. How'd I Do?
  • 8. Conflicting Objectives
  • 9. Back to the CheckOut
  • 10. Hashes vs. Classes
  • 11. Sorting it Out
  • 12. Best Sellers
  • 13, Counting Code Lines
  • 14. Tom Swift Under Milk Wood
  • 15. Playing With Bits
  • 16. Business Rules
  • 17. More Business Processing
  • 18. Transitive Dependencies
  • 19. Word Chains
  • 20. Klondike
  • 21. Simple Lists
Lijit Search
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.