Skip to content

Instantly share code, notes, and snippets.

@armw4
Last active December 31, 2015 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armw4/8001044 to your computer and use it in GitHub Desktop.
Save armw4/8001044 to your computer and use it in GitHub Desktop.
A little brain teaser for you my dear apprentice...

###by Antwan Wimberly###

Binary logarithm of some number y is some number x, where x is 2 raised to x

2 ^ x = y

x | y
-----
0 | 1
1 | 2
2 | 4
3 | 8
4 | 16
5 | 32

You challenge is to figure out how x relates to y based on the base, which is the number 2 in this case. It's certainly not y / base because 32 / 2 != 5, it equals 16. So throw that one out and try again. What's the pattern here? Can you do it without brute force? What if you counted the number of times you had to multiply 2 by itself (2) in order to get to that number? Well...that'd be brute force now wouldn't it? Here's a one liner in ruby:

binary_logarithm = 0; binary_logarithm+=1 while 2**binary_logarithm < 32; puts "binary logarithm of 32 is #{binary_logarithm}";

The above is perfectly valid ruby syntax, but to be fair, it should really look like this:

binary_logarithm = 0;

binary_logarithm+=1 while 2** binary_logarithm < 32

puts binary_logarithm

Lol. There has to be a simpler way though right? A pattern of some sort, yea? Do you remember how you solved for a variable back in high school? The goal was always to get the variable isolated by itself and push everything else to the opposite side by applying the inverse operation. Lets take an example; say we've got the following:

x - 9 = y

And say we have to solve for x. How do we get there? We need it to stand alone on an island by getting rid of the number 9. How do we do that? Well...what's the opposite of subtraction? Addition! Therefore

x = y + 9

How do we prove that? Well...

x  - 9 = y
17 - 9 = 8

x  = y + 9
17 = 8 + 9

It works!! So...if we can do this then we can do...that! Just figure out what's the inverse of raising 2 to x. Well...history tells me that it normally has something to do with this funny looking sign that looks like the letter v attached to a capital I that's been rotated 90 degrees. For example...

the opposite of raising 2 to the power of 4 is taking the SQUARE ROOT of that value (4).

the opposite of raising 2 to the power of 3 it taking the CUBED ROOT of that value (8).

So how do we find the nth ROOT of some number y, while honoring our base (the number 2), in the process? Lol...all that work and we're back to square one...or so it seems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment