Code Puzzle #1 - What numbers under one million are divisible by their reverse?
The other day I got stuck waiting in a slow line at the store all hopped up on espresso and breakbeat, so I started trying to think of numbers which are evenly divisible by their reverse. Example: If 721 were divisble by 127, I'd have a match. Yes, I'm aware this is not normal behavior.
I quickly convinced myself that there were no numbers like that under 1000, but I was sure there had to be some higher numbers like that. Seemed like a pretty simple thing to figure out with a few lines of code, which I did once I got home. As I recently discussed with Phil, I think that sort of thing is a lot more interesting as a quiz then a boring post with a block of code, so here it is.
I'm giving this quiz a difficulty rating of: Easy Peasy
First, some simple ground rules:
- We don't count numbers which are palindromes. Of course 42124 is divisible by 42124, but where's the fun in that?
- We don't count numbers which end in 0, since their reverse will drop the zero and that's not what I'm looking for. If we didn't have that rule, we'd have to deal with things like 2000 -> 0002, which only works because we'd drop the leading zeros. Lame.
Now, the quiz:
- (Answer this first) Take a guess - how many numbers do you think there are between 1 and 1 million that are divisible by their reverse. Any? Fifty? A thousand?
- Write a program to find the answer. I use C#, but feel free to use any programming language you'd like.
- If you use .NET, you may run into a limitation in the framework that's a little surprising.1 What is the limitation?
I made a tactical error on my recent SQL Puzzle by posting the answer immediately after the question, assuming people would still take the quiz. I was shown the error of my ways by friends who said they weren't going to bother figuring it out when the answer was right there... No honor system this time - I'll post my solution 24(ish) hours from now.
UPDATE: See my solution and recap here.
1Don't worry, you shouldn't have any problem finding some code to work around this.