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.

About these ads

Pages: 1 2

11 Responses to “Same Five Digits”

  1. […] today’s Programming Praxis exercise, our goal is to solve a numeric riddle. Let’s get started, shall […]

  2. My Haskell solution (see http://bonsaicode.wordpress.com/2011/04/19/programming-praxis-same-five-digits/ for a version with comments):

    import Data.List
    import qualified Data.List.Key as K
    
    squares :: [Integer]
    squares = filter (all (`elem` "12345") . show) .
              takeWhile (< 100000) $ map (^2) [100..]
    
    sameFive :: Maybe (Integer, Integer, Integer)
    sameFive = fmap (fst . head) . find (null . tail) . K.group snd $ K.sort snd
               [ ((a,b,c), findIndex (== 1) dc)
               | a <- squares, b <- squares, a < b, c <- squares, b < c
               , let dc = map length . group . sort $ show =<< [a,b,c]
               , sort dc == [1..5], and $ zipWith (/=) dc [1..5]
               ]
    
    main :: IO ()
    main = print $ sameFive == Just (12321,33124,34225)
    
  3. programmingpraxis said

    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.

  4. slabounty said

    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.

    # squares is a hash with the square and another hash that is the digits and their count. For
    # example { 12321 => {"1"=>2, "2"=>2, "3"=>1}, 33124 => { "3"=>2, "1"=>1, "2"=>1, "4"=>1}... }
    squares = {}
    
    # We're going from 100 (squared to 10000) to 236 (squared to 55696, first number greater than all
    # fives. See http://programmingpraxis.com/2011/04/19/same-five-digits/ for the explanation of
    # why this is true and why we're only searching for numbers containing the digits 0-5.
    100.upto(236) do |n|
        h = Hash.new(0)
        square = (n**2)
        square.to_s.each_char { |d| h[d] += 1 }
        squares[square] = h if h.inject(true) { |r, pair| r && ("012345".index(pair[0]) != nil) }
    end
    
    # solution_hash will look be an array of the three squares and a hash of digit counts of the three numbers.
    # For example { [12321, 12544, 55225] => {"1"=>"3", "2"=>"5", "3"=>"1", "5"=>"4", "4"=>"2"}, 
    #               [12321, 33124, 34225] => {"1"=>"3", "2"=>"5", "3"=>"4", "4"=>"2", "5"=>"1"} ... }
    solution_hash = {}
    
    # We need to go through the squares hash and generate a new hash, h2 which contains a merge of
    # three hashes derived by adding the values together in the three. After we have this
    # new hash, we convert the values strings for final processing. Our final processing is a series
    # of checks. First make sure that the number of keys is five and that the number of values is five.
    # Then make sure that the keys and values contain the same digits, and finally make sure that none
    # of the values is equal to its key. If all of these are true then add this solution to the solution
    # hash if it's not already there.
    squares.each_pair do |k1, v1|
        squares.each_pair do |k2, v2|
            squares.each_pair do |k3, v3|
                next if (k1 == k2) || (k2 == k3) || (k1 == k3)
                h2 = v1.merge(v2){|k4, oldval1, newval1| newval1+oldval1}.merge(v3){|k5, oldval2, newval2| newval2+oldval2}
                h2.each_pair {|k6, v6| h2[k6] = v6.to_s }
                if h2.length == 5 && h2.values.uniq.length == 5 && 
                   h2.keys.sort.eql?(h2.values.sort) && h2.inject(true) {|r, pair| r && (pair[0] != pair[1]) }
                    solution_hash[[k1, k2, k3].sort] = h2 if !solution_hash.has_key?([k1, k2, k3].sort)
                end
            end
        end
    end
    
    # Display all the solutions.
    solution_hash.each_pair do |k, v|
        puts "Solution: #{k} #{v}"
    end
    

    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″}

  5. Jussi Piitulainen said

    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.)

    candidate(Fifteen) :-
            between(99, 300, Q), digits(Q, Fifteen-Ten), Q1 is Q + 1,
            between(Q1, 300, R), digits(R, Ten-Five),    R1 is R + 1,
            between(R1, 300, S), digits(S, Five-[]),
            counts(Fifteen, Counts),
            setof(C, D^( member(D-C, Counts) ), [1,2,3,4,5]),
            \+ member(N-N, Counts).
    
    digits(M, [D1,D2,D3,D4,D5|More]-More) :-
            M5 is M * M,       digit(M5, M4, D5), digit(M4, M3, D4),
            digit(M3, M2, D3), digit(M2, M1, D2), digit(M1, 0,  D1).
    
    digit(M, M1, D) :- M1 is M // 10, D is M mod 10, between(1, 5, D).
    
    counts(Candidate, Counts) :-
            setof(D-C, ( between(1,5,D), count(D,Candidate,C) ), Counts).
    
    count(Digit, Candidate, Count) :-
            bagof(Digit, member(Digit, Candidate), Occurrences),
            length(Occurrences, Count),
            between(1, 5, Count).
    

    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.

    ?- candidate(X), counts(X,Cs), member(D-1,Cs), write(D-X), nl, fail.                                                       
    3-[1, 2, 3, 2, 1, 1, 2, 5, 4, 4, 5, 5, 2, 2, 5]                                                                            
    5-[1, 2, 3, 2, 1, 3, 3, 1, 2, 4, 3, 4, 2, 2, 5]                                                                            
    3-[1, 2, 3, 2, 1, 4, 4, 5, 2, 1, 5, 5, 2, 2, 5]                                                                            
    3-[1, 2, 3, 2, 1, 5, 2, 4, 4, 1, 5, 5, 2, 2, 5]                                                                            
    3-[1, 2, 5, 4, 4, 3, 4, 2, 2, 5, 4, 4, 5, 2, 1]                                                                            
    3-[1, 2, 5, 4, 4, 3, 4, 2, 2, 5, 5, 2, 4, 4, 1]                                                                            
    3-[3, 4, 2, 2, 5, 4, 4, 5, 2, 1, 5, 2, 4, 4, 1]                                                                            
    false.                                                                                                                     
    
  6. slabounty said

    @Jussi, Nice! I was thinking this would be a good Prolog problem as I was working on my version.

  7. Dan Harasty said

    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.

    
    from itertools import combinations
    
    # 100*100 is the smallest 5-digit perfect square (10000)
    # 316*316 is the largest 5-digit perfect square (99856)
    # we'll be doing alot of string processing on them, 
    # so convert the square to a string
    five_dig_squares = [str(x*x) for x in range(100, 317)]
    
    print("found %d 5-digit perfect squares" % len(five_dig_squares))
    
    candidates_found = 0
    
    # use combinations() from Python's itertools library to cycle
    # through the squares in unique three-up sets
    for set_of_three in combinations(five_dig_squares, 3):
    
    	# string-concatenate the three squares
    	fifteen_digits  = "".join(set_of_three)
    	
    	# pass the string to set()
    	# since fifteen_digits is a string, and string
    	# is a sequence, the set() constructor naturally
    	# breaks the sequence up, and keeps only unique occurances
    	unique_digits = set(fifteen_digits)
    
    	# if the size of the unique_digits set is not exactly 5,
    	# then consider the next set_of_three
    	if len(unique_digits) != 5:
    		continue
    	
    	# create a dictionary where the key is the character, and the value is the count
    	count_digits = dict((c, str(fifteen_digits.count(c))) for c in unique_digits)
    
    	# if any of the keys (a given digit) match the values (that digit's number
    	# of occurrences), then this is not a viable candidate
    	if sum([1 for k, v in count_digits.items() if k == v]) > 0: continue
    		
    	# create a set of the count values (the numbers of occurrences)
    	count_digits_values = set(count_digits.values())
    	
    	# and if the set digits we found match the set of occurrences, then
    	# we have a candidate... print out the info
    	if unique_digits == count_digits_values:
    		print(set_of_three)
    		print(unique_digits)
    		print(count_digits)
    		print()
    		candidates_found += 1
    
    print("candidates found: %d" % candidates_found)
    
    

    Yields the seven candidates from 217 possibilities:

    found 217 5-digit perfect squares
    ('12321', '12544', '55225')
    {'1', '3', '2', '5', '4'}
    {'1': '3', '3': '1', '2': '5', '5': '4', '4': '2'}
    
    ('12321', '33124', '34225')
    {'1', '3', '2', '5', '4'}
    {'1': '3', '3': '4', '2': '5', '5': '1', '4': '2'}
    
    ('12321', '44521', '55225')
    {'1', '3', '2', '5', '4'}
    {'1': '3', '3': '1', '2': '5', '5': '4', '4': '2'}
    
    ('12321', '52441', '55225')
    {'1', '3', '2', '5', '4'}
    {'1': '3', '3': '1', '2': '5', '5': '4', '4': '2'}
    
    ('12544', '34225', '44521')
    {'1', '3', '2', '5', '4'}
    {'1': '2', '3': '1', '2': '4', '5': '3', '4': '5'}
    
    ('12544', '34225', '52441')
    {'1', '3', '2', '5', '4'}
    {'1': '2', '3': '1', '2': '4', '5': '3', '4': '5'}
    
    ('34225', '44521', '52441')
    {'1', '3', '2', '5', '4'}
    {'1': '2', '3': '1', '2': '4', '5': '3', '4': '5'}
    
    candidates found: 7
    

    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!

  8. Graham said

    An uncommented Python version; I’ll put up a github gist later if people are interested.

    #!/usr/bin/env python
    from collections import Counter
    from itertools import combinations
    
    def histogram(triple):
        return Counter(int(d) for d in ''.join(str(x) for x in triple))
    
    def meets_criteria(triple):
        hist = histogram(triple)
        digits, counts = set(hist.keys()), set(hist.values())
        return digits == counts and not any(hist[k] == k for k in hist)
    
    def unique_single(triples):
        digits = {i: [] for i in xrange(1, 6)}
        for t in triples:
            hist = histogram(triple)
            digits[min(hist.iterkeys(), key=lambda k: hist[k])].append(t)
        return filter(lambda v: len(v) == 1, digits.itervalues())[0][0]
    
    def main():
        nums = (n**2 for n in xrange(100, 236) if set(str(n**2)).issubset('12345'))
        return unique_single(t for t in combinations(nums, 3) if meets_criteria(t))
    
    if __name__ == "__main__":
        print main()
    
  9. arturasl said

    My solution in vimscript:

    let aInitial = []
    for i in range(100, 236)
    	let aTimes = split(i * i, '\zs')
    	if max(aTimes) <= 5 && count(aTimes, '0') == 0
    		call add(aInitial, aTimes)
    	endif
    endfor
    
    let aMatrix = []
    for i in range(0, len(aInitial) - 1)
    	for j in range(i + 1, len(aInitial) - 1)
    		for k in range(j + 1, len(aInitial) - 1)
    			call add(aMatrix, [aInitial[i], aInitial[j], aInitial[k]])
    		endfor
    	endfor
    endfor
    
    let aNewMatrix = []
    for aLine in aMatrix
    	let aNewLine = copy(aLine)
    	let aCounts = []
    
    	for i in range(1, 5)
    		call add(aCounts, count(extend(extend(copy(aLine[0]), aLine[1]), aLine[2]), string(i)))
    		call extend(aNewLine, [[i, aCounts[i - 1]]])
    	endfor
    
    	let bAdd = 1
    
    	for i in range(1, 5)
    		if aCounts[i - 1] == i || count(aCounts, i) != 1
    			let bAdd = 0
    		endif
    	endfor
    
    	if bAdd
    		call add(aNewMatrix, aNewLine)
    	endif
    endfor
    
    let aUniqueOnes = [[0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
    for i in range(0, len(aNewMatrix) - 1)
    	for j in range(3, 7)
    		if aNewMatrix[i][j][1] == 1
    			let aUniqueOnes[j - 3][0] += 1
    			let aUniqueOnes[j - 3][1] = i
    			break
    		endif
    	endfor
    endfor
    
    for a in aUniqueOnes
    	if a[0] == 1
    		echo 'Output: ' . string(aNewMatrix[a[1]])
    	endif
    endfor
    
    Output: [['1', '2', '3', '2', '1'], ['3', '3', '1', '2', '4'], ['3', '4', '2', '2', '5'], [1, 3], [2, 5], [3, 4], [4, 2], [5, 1]]
    
  10. Sefer said

    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

  11. This Forth program is not very elegant, but it finds the single solution in about 0.7 seconds.

    
    0 VALUE n          PRIVATE
    0 VALUE used-once  PRIVATE
    CREATE pfs PRIVATE    #220 CELLS ALLOT
    CREATE xi  PRIVATE     #10 CELLS ALLOT
    CREATE sol PRIVATE #10 4 * CELLS ALLOT ( count a b c | used-once rows )
    
    :NONAME  CLEAR n  #236 #105 DO  I I *  pfs n CELL[] !  1 +TO n  LOOP ; EXECUTE
     
    : DISSECT ( n -- ) 5 0 DO  #10 /MOD 1 ROT xi []CELL +!  LOOP DROP ; PRIVATE 
    
    : NOT-EXACT? ( n -- bool ) 
    	LOCAL n
    	xi n CELL[] @
    	n 0=  n 5 > OR IF  0<> EXIT  ENDIF \ number of 0,6,7,8,9's should be 0
    	0= ; PRIVATE 
    
    : HAVE-54321? ( -- bool )
    	TRUE 6 1 DO  FALSE  
    		     6 1 DO  xi I CELL[] @ J = 
    		     	     I J <> ( no digit is used its own number of times )
    		     	     AND IF  J 1 = IF  I TO used-once  ENDIF 
    			     	     DROP TRUE LEAVE  
    			      ENDIF  
    		       LOOP  
    		     AND  
    	       LOOP ; PRIVATE
    
    : 5DIGITS-TESTS? ( -- bool ) 
    	#10 0 DO  I NOT-EXACT? IF  FALSE UNLOOP EXIT  ENDIF  LOOP 
    	HAVE-54321? ; PRIVATE
    
    : UNEQUAL? ( i j k -- a b c TRUE | FALSE )
    	pfs []CELL @ LOCAL a
    	pfs []CELL @ LOCAL b
    	pfs []CELL @ LOCAL c
    	a b =  a c = OR  b c = OR  
    	0= IF  a b c TRUE 
    	 ELSE  FALSE 
    	ENDIF ; PRIVATE
    
    : SORTED! ( a b c -- )
    	used-once 4 * sol []CELL LOCALS| addr c b a |
    	a b < IF  a  b TO a  TO b  ENDIF
    	a c < IF  a  c TO a  TO c  ENDIF
    	b c < IF  b  c TO b  TO c  ENDIF
    	addr 1 CELL[] @ a <>
    	addr 2 CELL[] @ b <> AND
    	addr 3 CELL[] @ c <> AND
    	   IF   1 addr +!
    	 	a addr 1 CELL[] !  
    		b addr 2 CELL[] ! 
    		c addr 3 CELL[] ! 
    	ENDIF ; PRIVATE
    
    : DISCARD ( -- u )
    	sol #10 4 * CELLS ERASE
    	n 0 ?DO  
    	n 0 ?DO  
    	n 0 ?DO  xi #10 CELLS ERASE
    		 pfs I CELL[] @ DISSECT
    		 pfs J CELL[] @ DISSECT
    		 pfs K CELL[] @ DISSECT
    		 5DIGITS-TESTS? IF  I J K UNEQUAL? IF  SORTED!  ENDIF  ENDIF  
    	   LOOP 
    	   LOOP 
    	   LOOP ; PRIVATE
    
    : .SOLUTION ( -- )
    	0 LOCAL addr
    	TIMER-RESET
    	DISCARD
    	6 1 DO  sol I 4 * CELL[] TO addr
    		addr @ 1 = IF  CR ." Solution = " 
    				  addr 1 CELL[] @ .  
    				  addr 2 CELL[] @ . 
    				  addr 3 CELL[] @ .
    		        ENDIF
    	  LOOP 
    	'(' EMIT .ELAPSED ')' EMIT ; 
    
    :ABOUT	CR ." Same Five Digits"
         	CR ." Try .SOLUTION " ;
    
    
    		.ABOUT -samefivedigits CR
    
    

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 643 other followers

%d bloggers like this: