Enigmatic Code

Programming Enigma Puzzles

Enigma 1332: Square pins

From New Scientist #2491, 19th March 2005

George, and his brothers Fred and Henry, are comparing the PIN codes they have been given for their new credit cards. Each is a four digit number, and none contain a zero digit. The last digit of Fred’s number is the same as the first digit of George’s, the last digit of George’s is the first digit of Henry’s, and the last digit of Henry’s is the first digit of Fred’s. Apart from these three pairs of different digits, no digit occurs more than once among the PINs.

The brothers have each calculated the sum of the squares of the four digits in their PIN and to their great surprise they found that the three totals are exactly the same.

What is the sum of the four digits in George’s PIN?

[enigma1332]

Advertisements

3 responses to “Enigma 1332: Square pins

  1. Jim Randell 14 April 2014 at 8:32 am

    This Python program runs in 35ms.

    from itertools import combinations
    from enigma import irange, printf
    
    # digits
    ds0 = set(irange(1, 9))
    
    # squares
    square = dict((d ** 2, d) for d in ds0)
    
    # choose 4 digits for Fred
    for F in combinations(ds0, 4):
      # compute the sum of the squares
      s = sum(x ** 2 for x in F)
    
      # chose three digits for George
      ds1 = ds0.difference(F)
      for G in combinations(ds1, 3):
        # which digit is shared between F and G
        fg = square.get(s - sum(x ** 2 for x in G), None)
        if fg not in F: continue
    
        # choose the digit shared between G and H
        s2 = sum(x ** 2 for x in ds1.difference(G))
        for gh in G:
          # find the digit shared between F and H
          fh = square.get(s - (s2 + gh ** 2), None)
          if fh not in F: continue
    
          Fs = set(F)
          Gs = set(G).union([fg])
          Hs = ds1.difference(G).union((fh, gh))
    
          sG = sum(x for x in G) + fg
          printf("sum(G) = {sG} [F={Fs} G={Gs} H={Hs}, fg={fg} gh={gh} fh={fh}]")
    

    Solution: The sum of the digits of George’s PIN is 20.

    The question gives us no way to distinguish George, Fred and Henry, so for any solution (A, B, C) any other permutation will also be a solution. Hence the six solutions my program finds. Fortunately the sums of the digits for all three PINs is the same, so there is only one possible answer to the problem.

    • geoffrounce 14 April 2014 at 12:21 pm

      Yes, a permutation solution confirms your answer.

      It also gives possible solutions for the pin numbers, after duplicate solutions are eliminated.
      ie
      George = 2945, Fred = 8732, Henry = 5618
      George = 2738, Fred = 5942, Henry = 8615
      George = 5942, Fred = 8615, Henry = 2738
      George = 5618, Fred = 2945, Henry = 8732
      George = 8732, Fred = 5618, Henry = 2945
      George = 8615, Fred = 2738, Henry = 5942

      # george a * * b
      # fred   c * * a
      # henry  b * * c
      
      from itertools import permutations
      
      for p in permutations((1,2,3,4,5,6,7,8,9)):
          a,b,c,d,e,f,g,h,i = p
          # build numbers
          george = b + d*10 + e*100 + a*1000
          fred = a + f*10 + g*100 + c*1000
          henry = c + h*10 + i*100 + b*1000
          # build sums of squares of digits of numbers
          sum_sq_g = b**2 + d**2 + e**2 + a**2
          sum_sq_f = a**2 + f**2 + g**2 + c**2
          sum_sq_h = c**2 + h**2 + i**2 + b**2
          if sum_sq_g == sum_sq_f == sum_sq_h:
              sum_g = b + d + e + a
              sum_f = a + f + g + c
              sum_h = c + h + i + b
              print( 'Sum = {} George = {}, Fred = {}, Henry = {}'\
                     . format(sum_g,george,fred,henry))
      
  2. Naim Uygun 14 April 2014 at 12:55 pm
    from itertools import permutations
    for a,b,c,d,e,f,g,j,k in permutations("987654321",9):
        abcd=a+b+c+d
        efga=e+f+g+a
        djke=d+j+k+e
        george=int(a)**2+int(b)**2+int(c)**2+int(d)**2
        fred=int(e)**2+int(f)**2+int(g)**2+int(a)**2
        henry=int(d)**2+int(j)**2+int(k)**2+int(e)**2
        if len({george,fred,henry})==1:
            gsum=int(a)+int(b)+int(c)+int(d)
            print(" Answer:",gsum,  " George PIN=",abcd," Fred PIN=",efga," Henry PIN=",djke)
            break
    

Leave a Comment

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

%d bloggers like this: