r/dailyprogrammer • u/fvandepitte 0 0 • Jan 20 '16
[2016-01-20] Challenge #250 [Intermediate] Self-descriptive numbers
Description
A descriptive number tells us how many digits we have depending on its index.
For a number with n digits in it, the most significant digit stands for the '0's and the least significant stands for (n - 1) digit.
As example the descriptive number of 101 is 120 meaning:
- It contains 1 at index 0, indicating that there is one '0' in 101;
- It contains 2 at index 1, indicating that there are two '1' in 101;
- It contains 0 at index 2, indicating that there are no '2's in 101;
Today we are looking for numbers that describe themself:
In mathematics, a self-descriptive number is an integer m that in a given base b is b digits long in which each digit d at position n (the most significant digit being at position 0 and the least significant at position b - 1) counts how many instances of digit n are in m.
As example we are looking for a 5 digit number that describes itself. This would be 21200:
- It contains 2 at index 0, indicating that there are two '0's in 21200;
- It contains 1 at index 1, indicating that there is one '1' in 21200;
- It contains 2 at index 2, indicating that there are two '2's in 21200;
- It contains 0 at index 3, indicating that there are no '3's in 21200;
- It contains 0 at index 4, indicating that there are no '4's in 21200;
Formal Inputs & Outputs
Input description
We will search for self descriptive numbers in a range. As input you will be given the number of digits for that range.
As example 3 will give us a range between 100 and 999
Output description
Print out all the self descriptive numbers for that range like this:
1210
2020
Or when none is found (this is very much possible), you can write something like this:
No self-descriptive number found
In and outs
Sample 1
In
3
Out
No self-descriptive number found
Sample 2
In
4
Out
1210
2020
Sample 3
In
5
Out
21200
Challenge input
8
10
13
15
Notes/Hints
When the number digits go beyond 10 you know the descriptive number will have trailing zero's.
You can watch this for a good solution if you get stuck
Bonus
You can easily do this by bruteforcing this, but from 10 or more digit's on, this will take ages.
The bonus challenge is to make it run for the large numbers under 50 ms, here you have my time for 15 digits
real 0m0.018s
user 0m0.001s
sys 0m0.004s
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
And special thanks to /u/Vacster for the idea.
EDIT
Thanks to /u/wboehme to point out some typos
6
u/gabyjunior 1 2 Jan 20 '16 edited Jan 22 '16
C, backtracking on each digit (started to work on the subject when the dailyprogrammer idea was submitted).
Instead of having a base in input, a list of digits is expected as program argument (to be able to manage base > 10). For standard base 10 list will be "0123456789".
The program will output all self descriptive numbers for base equal to or lower than the number of digits provided.
The backtracking takes advantage of 2 constraints on self descriptive numbers to reduce the search space:
Source code
EDIT new version more simple and faster (backtracking from last to first digit, because of higher digit values tested first the search backtracks sooner)
Example for base = 36
3
u/IisCoCo Jan 20 '16
This is a great challenge but i feel like when i'm trying to make a version that runs in under 50ms I am creating more of a pattern builder that fits the requirements than a self-descriptive number finder that looks for the requirements
2
u/fvandepitte 0 0 Jan 20 '16
My own solution in Haskell
2
u/IisCoCo Jan 20 '16
The way i tackled the problem was very linear. simply cycle though all the number with the inputted amount of digits. then for each digit count how many of that digit's index are contained within the number.
There is a lot of casting between strings and integers.
Java
package main;
2
u/a_Happy_Tiny_Bunny Jan 20 '16
Haskell
I'd figure I'd at least implement a näive solution before going to bed.
The compiled program takes the input as a command line argument. It's not meant to be fast, not even for a näive approach.
2
u/Godspiral 3 3 Jan 20 '16 edited Jan 20 '16
In J, first filter partitions
rearanging
0.0940718 1.0021e7 (9 ms)
the repeatperm part is the relatively slow part. Creates permutations with repeats and tests each one for validity. There is likely a more complicated shortcut.
2
u/Whats_Calculus Jan 20 '16 edited Jan 20 '16
Clojure, mostly done but I haven't completed the output formatting yet.
The main idea is that you can generate the possible nonzero digits by examining integer partitions and then permute them with some zeros and see what works.
If I generated the unique partition lengths instead of generating every partition (orders of magnitude difference) the program should run almost instantly, but I don't have time mess around with it.
2
u/ChazR Jan 20 '16
Haskell, brute force. It should be simple to do a tiny bit of maths to eliminate vast swathes of the search space, and I may do that later.
1
u/cheers- Jan 20 '16
Question: beyond the 10th (0-9) digit every other digit doesnt have meaning(doesnt provide description to quote the challenge)?
is it just padding(trailing zeroes)?
Should we change base( eg input 16 switch from 10 to hex) ?
1
u/cheers- Jan 20 '16 edited Jan 20 '16
Java
What I've figured out:
10 digits solution
my code works from 1 to 10 digits number and uses a recursive brute force algorithm to find all the solutions.
1
u/usedthrone Jan 20 '16 edited Jan 20 '16
PHP
Well, I believe I managed to get a working program however after 6 digits the program just... crawls. Any suggestions?
4 digits takes - 0.03923
5 digits takes - 0.45555
6 digits takes - 4.83470
7 digits takes - fatal error over 30 seconds
1
u/holygawdinheaven Jan 20 '16
Here's my JavaScript solution. With the addition of the possibleCheck function I was able to get it to run 8 it about 30 seconds... I was afraid to try the higher numbers. Is there a better way than iterating through every number? I noticed that numbers whose digits add up to the number of digits in themselves are always at least 9 apart. I threw in a line that iterates by 9 if one is found, but this only saved me about 3 seconds on the 8. Feedback is welcome.
1
u/sepehr500 Jan 25 '16
Wrote using C#. Pretty slow. Any Tips?
3
u/CleverError Jan 20 '16
My solution in Swift 2
It recursively builds the number and the count of digit occurrences at the same time then it just checks to see if they are equal at the end.
It currently only generates numbers with base10 digits. I'll try and update it when I get more time.
Output
2
u/hatbeat Jan 20 '16
Python 3
Used /u/cheers-'s assumptions
2
u/TheBlackCat13 Jan 22 '16 edited Jan 22 '16
Some rules I have figured out. I will use
nas the number of digits:Based on these rules, here is my semi-smart, semi-brute-force Python 3 approach to finding all results with length < 25:
And here are the results, with benchmarks:
1
u/vincchan Jan 20 '16
Javascript
My first challenge. I know this was done horribly. All suggestions to improve this are welcome
1
u/aidanharris1 Jan 20 '16
A very naive approach in Python3 that doesn't try to be clever:
Python3
1
u/Newtzor Jan 20 '16 edited Jan 20 '16
Scrubby semi-bruteforced Python solution... couldn't handle 10 digits in a feasible amount of time but I think I could have done worse. I'll probably spend a bit more time on this and come back later with an edit.
Outputs:
Still diving in to the world of programming, so feedback is very much appreciated!!
1
u/Eggbert345 Jan 20 '16 edited Jan 20 '16
Golang solution. I watched the video in the hints which pointed me to the fast solution. Also my numbers aren't technically correct, since I'm just incrementing on the ASCII chart. So ':' is 10, ';' is 11 etc.
Output:
EDIT: My times for finding the 15 digit solution, to compare with times in challenge posting
real 0m0.006s
user 0m0.001s
sys 0m0.004s
1
u/quails4 Jan 21 '16 edited Jan 21 '16
My solution in the D programming language, if anyone who knows D well has and comments or suggestions I'm all ears.
I use a recursive function to perform a depth first search and "collect" correct solutions as I go. To get it to run in a reasonable speed I added some constraint checks so it can exit branches early and I reuse the same array throughout rather than duplicating.
I was going to try some more optimisations but it's already running the 15 digit challenge in ~30ms in Debug and ~8ms in release.
1
u/downiedowndown Jan 21 '16
Swift 2 Not tried for numbers of greater than 10 digits yet, but I have the other working although slowly. Any tips welcome.
Output with a number of digits as 5:
1
u/FrankRuben27 0 1 Jan 22 '16
Solution in Go, tested up to 15 digits:
1
u/datgohan Jan 25 '16
Python. Would appreciate any C&C :) trying to learn better methods.
1
u/jeaton Jan 26 '16
python 3:
0
u/[deleted] Jan 20 '16
[deleted] — view deleted comment