Same Five Digits
April 19, 2011
[Today’s exercise was written by guest author Bob Miller. Bob has been writing system software for Unix since the VAX was new and shiny, and his current hobby is writing Scheme interpreters. Suggestions for exercises are always welcome, or you may wish to contribute your own exercise; feel free to contact me if you are interested.]
Enigma is a weekly column in New Scientist. Every week it has a new puzzle. Some of the Enigma puzzles could be solved using a computer.
A recent puzzle, Enigma Number 1638, is in that category:
I have written down three different 5-digit perfect squares, which between them use five different digits. Each of the five digits is used a different number of times, the five numbers of times being the same as the five digits of the perfect squares. No digit is used its own number of times. If you knew which digit I have used just once you could deduce my three squares with certainty.
What are my three perfect squares?
Your task is to write a program that finds and prints the three perfect squares. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
[…] today’s Programming Praxis exercise, our goal is to solve a numeric riddle. Let’s get started, shall […]
My Haskell solution (see http://bonsaicode.wordpress.com/2011/04/19/programming-praxis-same-five-digits/ for a version with comments):
Here is my solution:
(list-of (list a b c s)
(x is (list-of x2
(x range 100 236) (x2 is (* x x))
(not (= (apply min (digits x2)) 0))
(< (apply max (digits x2)) 6)))
(a in x) (b in x) (c in x) (< a b) (< b c)
(d is (sort < (mappend digits (list a b c))))
(s is (uniq-c = d))
(= (length (unique = (sort < (map cdr s)))) 5)
(equal? (unique = d) (unique = (sort < (map cdr s))))
(ok? s))
Let’s look at that slowly. X is a list of all five-digit squares that contain only the digits 1 through 5. The inner x ranges from 100 (because 1002 is the smallest 5-digit number) to 236 (because 2362 is the smallest square greater than 55555). Then the predicate (< 0 (apply max (digits x2)) 6) passes only those numbers with the digits 1 through 5. There are 9 such numbers, the squares of 111, 112, 115, 182, 185, 188, 211, 229, and 235.
On the next line, the three "in" clauses form the cross product of the nine squares, a total of 93=729 triplets. The remaining two clauses on that line eliminate duplicates, reducing the number of triplets from 729 to 84, which by the binomial theorem is the number of ways 3 items can be chosen from a list of 9:
(define (choose n k)
(if (zero? k) 1
(* n (/ k) (choose (- n 1) (- k 1)))))
> (choose 9 3)
84
Next we want to calculate the “signature” of the digit counts in the solution. We calculate the fifteen digits in d, and the signature of the counts in s.
Now we return to the puzzle. Each of the five digits is used a different number of times, which is tested by the predicate (= (length (unique = (sort < (map cdr s)))) 5). The counts have to be the same as the digits, which is tested by the predicate (equal? (unique = d) (unique = (sort < (map cdr s)))); the solution works if you omit that predicate, but I couldn't articulate a clear reason why, so I included it. Finally, no digit may be used its own number of times, which is tested by the
ok?
function:(define (ok? s)
(cond ((null? s) #t)
((= (caar s) (cdar s)) #f)
(else (ok? (cdr s)))))
At this point the list comprehension gives us seven remaining triplets:
((12321 12544 55225 ((1 . 3) (2 . 5) (3 . 1) (4 . 2) (5 . 4)))
(12321 33124 34225 ((1 . 3) (2 . 5) (3 . 4) (4 . 2) (5 . 1)))
(12321 44521 55225 ((1 . 3) (2 . 5) (3 . 1) (4 . 2) (5 . 4)))
(12321 52441 55225 ((1 . 3) (2 . 5) (3 . 1) (4 . 2) (5 . 4)))
(12544 34225 44521 ((1 . 2) (2 . 4) (3 . 1) (4 . 5) (5 . 3)))
(12544 34225 52441 ((1 . 2) (2 . 4) (3 . 1) (4 . 5) (5 . 3)))
(34225 44521 52441 ((1 . 2) (2 . 4) (3 . 1) (4 . 5) (5 . 3))))
I was going to invert the
s
matrix to determine the correct answer, but it is obvious by inspection that there is only one triplet where the digit used once is unique, and I am lazy. The solution to the puzzle is the triplet 1112 = 12321, 1822 = 33124, and 1852 = 34225.I used list comprehensions, digits, unique, uniq-c, and mappend from the Standard Prelude. You can run the program at http://programmingpraxis.codepad.org/ujS7gS9I.
Here’s a ruby version with some of the ideas from above incorporated. In a couple of places I use inject() to figure out if something is true for all elements of a hash. In this case inject() will return an array of two values (a pair) with the key the first element and the value as the second element. In both cases we initialize with “true” and then “and” with our test.
Here’s the results …
Solution: [12321, 12544, 55225] {“1″=>”3”, “2”=>”5″, “3”=>”1″, “5”=>”4″, “4”=>”2″}
Solution: [12321, 33124, 34225] {“1″=>”3”, “2”=>”5″, “3”=>”4″, “4”=>”2″, “5”=>”1″}
Solution: [12321, 44521, 55225] {“1″=>”3”, “2”=>”5″, “3”=>”1″, “4”=>”2″, “5”=>”4″}
Solution: [12321, 52441, 55225] {“1″=>”3”, “2”=>”5″, “3”=>”1″, “5”=>”4″, “4”=>”2″}
Solution: [12544, 34225, 44521] {“1″=>”2”, “2”=>”4″, “5”=>”3″, “4”=>”5″, “3”=>”1″}
Solution: [12544, 34225, 52441] {“1″=>”2”, “2”=>”4″, “5”=>”3″, “4”=>”5″, “3”=>”1″}
Solution: [34225, 44521, 52441] {“3″=>”1”, “4”=>”5″, “2”=>”4″, “5”=>”3″, “1”=>”2″}
Let’s see what Prolog looks like when formatted as lang=”css”.
It’s been many years since I used Prolog, but I installed
SWI-Prolog for this now and found its help system helpful.
I didn’t bother to find the least upper bound for the square
roots. It is easy to see that 300 is a safe choice. (I even
started at 99 instead of 100 to make the columns align.)
I got one constraint wrong at first – the one that each of the
five counts be used – and thought the unique single digit was 4.
After correcting that, the following interaction prints the
possible pairs of the single digit and the sequence of the
fifteen digits, from which the three numbers can be read.
@Jussi, Nice! I was thinking this would be a good Prolog problem as I was working on my version.
I love using Python’s built-in set() object to “uniquify” and also to compare if two sequences have the same stuff irrespective of order. And, as much fun as it to write an combination-generator, here I rely on the combinations() function from Python’s itertools library.
Yields the seven candidates from 217 possibilities:
Visual inspection shows 6 of these 7 candidates have the digit 3 occurring only once… so the last clue implies that the answer must be the set where that is not 1 of those 6:
(‘12321’, ‘33124’, ‘34225’)
Fun!
An uncommented Python version; I’ll put up a github gist later if people are interested.
My solution in vimscript:
I too found seven such numbers (and not one as specified).
Another optimization may be that none of the numbers can contain the zero digit (since no appearing number can appear zero times). That should cut down overall runtime a bit further.
12321 12544 55225
3 = 1
2 = 5
4 = 2
1 = 3
5 = 4
12321 33124 34225
3 = 4
2 = 5
4 = 2
1 = 3
5 = 1
12321 44521 55225
3 = 1
2 = 5
4 = 2
1 = 3
5 = 4
12321 52441 55225
3 = 1
2 = 5
4 = 2
1 = 3
5 = 4
12544 34225 44521
3 = 1
2 = 4
1 = 2
4 = 5
5 = 3
12544 34225 52441
3 = 1
2 = 4
1 = 2
4 = 5
5 = 3
34225 44521 52441
3 = 1
2 = 4
1 = 2
4 = 5
5 = 3
This Forth program is not very elegant, but it finds the single solution in about 0.7 seconds.