Enigmatic Code

Programming Enigma Puzzles

Enigma 1300: Numeros triangulares

From New Scientist #2458, 31st July 2004

This puzzle has appeared as an Enigma puzzle in both English and French versions, but incredibly it also works in Portuguese.

Triangular numbers are those that fit the formula n×(n+1)/2, like 1, 3, 6 and 10. In the following statement digits have been consistently replaced by capital letters, different letters being used for different digits: UM, TRES, SEIS, DEZ are all triangular numbers, none of which starts with a zero.

Which numbers are represented (in this order) by UM, TRES, SEIS, and DEZ?

Note: I am waiting for a phone line to be connected at my new house, so I only have sporadic access to the internet at the moment.

[enigma1300]

Advertisements

3 responses to “Enigma 1300: Numeros triangulares

  1. Jim Randell 20 August 2014 at 8:31 am

    This Python program runs in 34ms.

    from itertools import count
    from enigma import printf
    
    # 2-, 3- and 4-digit triangular numbers
    ts = { 2: [], 3: [], 4: [] }
    t = 0
    for n in count(1):
      t += n
      s = str(t)
      l = len(s)
      if l < 2: continue
      if l > 4: break
      ts[l].append(s)
    
    # choose a value for UM
    for UM in ts[2]:
      ds1 = set(UM)
      if len(ds1) != 2: continue
    
      # choose a value for DEZ
      for DEZ in ts[3]:
        ds2 = ds1.union(DEZ)
        if len(ds2) != 5: continue
        E = DEZ[1]
    
        # choose a value for TRES
        for TRES in ts[4]:
          if TRES[2] != E: continue
          ds3 = ds2.union(TRES)
          if len(ds3) != 8: continue
          S = TRES[3]
    
          # choose a value for SEIS
          for SEIS in ts[4]:
            if SEIS[1] != E: continue
            if not(SEIS[0] == SEIS[3] == S): continue
            ds4 = ds3.union(SEIS)
            if len(ds4) != 9: continue
    
            printf("UM={UM} DEZ={DEZ} TRES={TRES} SEIS={SEIS}")
    

    Solution: UM=45, TRES=2701, SEIS=1081, DEZ=903.

  2. geoffrounce 20 August 2014 at 10:21 am

    A permutation solution gives the same answer:

    def is_tri(x):
        t = 8 * x + 1
        u = int((8 * x + 1) ** 0.5 + 0.5)
        return t == u ** 2
    
    from itertools import permutations
    
    for p in permutations ('1234567890', 2):
      u,m = p
      if u != '0' : um = int(u + m)
      if is_tri(um):
        q1 = set('1234567890') - set(p)
        for p2 in permutations(q1, 4):
          t,r,e,s = p2
          if t != '0' and s != '0':
            tres = int(t + r + e + s)
            if is_tri(tres):
              q2 = set(q1) - set(p2)
              for p3 in permutations(q2, 3):
                i,d,z = p3
                if d != '0':
                  seis = int(s + e + i + s)
                  if is_tri(seis):
                    dez = int(d + e + z)
                    if is_tri(dez):
                      print('UM={},TRES={},SEIS={},DEZ={}'.format(um,tres,seis,dez))
    
    
  3. Naim Uygun 20 August 2014 at 10:52 am
    #UM= 45 TRES= 2701 SEIS= 1081 DEZ= 903
    from itertools import permutations
    for u,m,t,r,e,s,i,d,z in permutations("9876543210",9):
        if '0' in [u,t,s,d]: continue
        um=u+m
        tres=t+r+e+s
        seis=s+e+i+s
        dez=d+e+z
        a=int(um)
        b=int(tres)
        c=int(seis)
        d=int(dez)
        n1=(-1+(1+8*a)**0.5)/2
        if n1 != int(n1): continue
        n2=(-1+(1+8*b)**0.5)/2
        if n2 != int(n2): continue
        n3=(-1+(1+8*c)**0.5)/2
        if n3 != int(n3): continue
        n4=(-1+(1+8*d)**0.5)/2
        if n4 != int(n4): continue
        print("UM=",um,"TRES=",tres,"SEIS=",seis,"DEZ=",dez)
    

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: