-
Website
http://20bits.com -
Original page
http://20bits.com/articles/facebook-job-puzzles-prime-bits/ -
Subscribe
All Comments -
Community
-
Top Commenters
-
prissypot13
3 comments · 1 points
-
Felix Purnama
4 comments · 1 points
-
hadley
2 comments · 1 points
-
adamheroku
2 comments · 3 points
-
twiss
2 comments · 1 points
-
-
Popular Threads
Why would you ever post the solution? I guess you're not interested in a job at Facebook.
Oh, yeah? Then did you not send anything to the Facebook?
My intention is to solve all their interesting puzzles and post the solutions. Why? Well, why not? De l'audace, encore de l'audace, toujours de l'audace.
Then I got busy with some of my other side projects since I got an email from Facebook saying they were finished finding Summer interns (that's what I was going for). Solving the solution, while fun, would not be efficient use of my time.
I guess the point in asking about posting the solutions is that it sort of undermines the effectiveness of the puzzle as a recruitment device. Plus it takes the fun out of figuring it out for ones' self. And by fun, I mean stress. ;)
Cheers!
Applying it to this problem, then, there are 6C2 6-bit numbers which have two bits set to one, 6C3 with three bits set to one, and 6C5 with five bits set to one. Since 1, 3, and 5 are the only primes less than 6 there are 6C2 + 6C3 + 6C5 6-bit numbers with a prime number of bits set to one.
It took me awhile to figure out the pascal's triangle caveat, but once I figured it out it was pretty simple.
My implementation generates the pascal array to the number of rows needed [number of significant bits]. I then 'condense' that array to create an array[x][y] that reads:
For all numbers equal to and less than 2^[x], there are occurances of [y] bits set on.
From there it was as simple as adding arrays, then cross referencing them against the 'arePrimes' array.
This was a very enjoyable problem!
I got lost in the part of solving the problem for other numbers rather than powers of 2. Could you explain it better? Can you give me other examples? why "-1"? why starting from 2C(3-1)?
Thanks
For powers other than 2 you need to break it down into several contiguous ranges. You have one range more than there are ones in the binary representation of the original number.
For example, if the number you're looking at is 10110 then the ranges are 00000-01111, 10000-10011, 10100-10101, and 10110. There are three ones and four ranges.
Each range corresponds to fixing one of the bits, starting from the left-most (most significant) bit. So the first range has no fixed bits, the second has one, the third has two, etc.
We subtract from our calculation the number of fixed bits. So, if the range we're looking at is 10000-10011 we're interested in numbers of the form 100xx. The only prime numbers that are suitable here are 2 and 3. That is, we can have at most three 1-bits.
But there's already a fixed bit (that one on the far left side), so if there are three 1-bits then that means there must be two 1-bits in the xx section. Thus we get 2C(3-1), because we're choosing the number of combinations in the xx section such that there are three 1-bits for the whole string.
Hope that helps,
Jesse