Numbers with Unique Digits

Difficulty: ★ ★ ★ ☆

I’m sure some eccentric term exists to describe a number where no digits repeat. Whether this concept has any mathematical relevance remains uncertain. But it’s the type of problem you can easily solve by writing a computer program.

Unique digit values would be numbers such as 456 or 1089637. None of the digits repeat. The value 1066, however, has repeating digits (two sixes), so it doesn’t count.

Obviously, these unique digit numbers can be only so large. Values ten digits wide must have repeating digits. In fact, I would guess that 9,876,543,210 is the highest unique digit value possible in base 10.

For this month’s Exercise, your task is to write code that outputs all values with unique digits in the range of zero through 10,000,000,000 (ten billion). Output all the unique values along with a tally of how many the program found. Yes, with all this output, the program takes several minutes to run. In fact, you may see it pause as values are scanned.

Here’s the last few lines from my solution:

9876543012
9876543021
9876543102
9876543120
9876543201
9876543210
8877691 unique values

This challenge earns three stars because multiple methods exist to scan the digits. I know which one I use, but I’m certain other methods are available. Searching this topic on the Interwebs shows similar challenges in other programming languages. Don’t cheat!

Try this challenge on your own. I’m eager to see what you come up with. You can compare your solution with mine.

Leave a Reply