Enigmatic Code

Programming Enigma Puzzles

Enigma 1427: Equal lengths

From New Scientist #2588, 27th January 2007

I challenged Harry and Tom to draw this diagram, with the angles ABD, BEC and CED each 90° and each side of the three triangles an integral number (less than 40) of centimetres long.

Enigma 1427

They each managed to do this.

In Harry’s version (AB+BC) was the same length as (AD+DC); in Tom’s version (AB+BE) was the same length as (AD+DE); CE was the same length in both versions.

What was the length of the perimeter of the quadrilateral ABCD in (a) Harry’s version, (b) Tom’s version?

[enigma1427]

Advertisements

2 responses to “Enigma 1427: Equal lengths

  1. Jim Randell 26 May 2013 at 9:40 am

    This Python program examines all possible combinations of Pythagorean triangles with hypotenuse less than 40 to find the answer. It runs in 51ms.

    from collections import defaultdict
    from itertools import product
    from enigma import irange, printf
    
    # find pythagorean triples with a hypotenuse less than 40
    ts = list()
    for c in irange(1, 39):
      for b in irange(1, c - 1):
        for a in irange(1, b - 1):
          if a * a + b * b == c * c:
            ts.append((a, b, c))
    
    # now find pairs of triangles with a matching non-hypotenuse side
    (Hs, Ts) = (list(), list())
    for (X, Y) in product(ts, repeat=2):
      # match the sides
      for (x, y) in product((0, 1), repeat=2):
        if X[x] != Y[y]: continue
        # sum the non-matching non-hypotenuse sides
        s = X[x ^ 1] + Y[y ^ 1]
        # find a matching Z
        for Z in ts:
          for z in (0, 1):
            if Z[z] != s: continue
            # check the conditions
            (AB, BC, BE, CE, AD, DC, DE) = (Z[z ^ 1], Y[2], Y[y ^ 1], X[x], Z[2], X[2], X[x ^ 1])
            p = AB + BC + DC + AD
            # for Harry:
            if (AB + BC == AD + DC):
              Hs.append((CE, p, (X, Y, Z)))
              printf("[H: {X} {Y} {Z} / {x} {y} {z} / CE={CE} p={p}]")
            # for Tom: AB + BE = AD + DE
            if (AB + BE == AD + DE):
              Ts.append((CE, p, (X, Y, Z)))
              printf("[T: {X} {Y} {Z} / {x} {y} {z} / CE={CE} p={p}]")
    
    # choose H and T
    for (H, T) in product(Hs, Ts):
      # CE is the same
      if H[0] != T[0]: continue
    
      printf("pH={H[1]} pT={T[1]} [H={H[2]} T={T[2]}]")
    

    Solution: (a) 90 cm; (b) 76 cm.

  2. Ahmet Saracoğlu 31 May 2013 at 1:07 pm

    In my machine which has 2.6 Ghz processor, the execution time is 16ms.

    import time
    time1=int(round(time.time()*1000))
    print(time1)
    Hip=[5,10,15,20,25,30,35,13,26,39,17,34,29,37]# These are the biggest side of the triangles
    
    import itertools as permt
    permus=list(permt.permutations(Hip,3))
    
    def FindTwoCircumferences():
        c,d,counter=0,0,0
        satisfier=[[0 for i in range(2)]for i in range(20)]
        circumference=dict()
        #print(satisfier)
        for i in range(len(permus)):
            e,b,f=permus[i][1],permus[i][0],permus[i][2]
            a=e+f-b
            if a in range(40):#Every side must be less than 40."
                root=pow(pow(e,2)-pow(a,2),0.5).real
                if int(root)==root:
                    cminusd=(b-f)*(b+f)//root#this equation comes from c^2+h^2=b^2 and d^2+h^2=f^2
                    c=(root+cminusd)//2# root denotes for c+d and c can be found easly by solving the eq.
                    d=root-c
                    h1,h2=pow(pow(b,2)-pow(c,2),0.5).real,pow(pow(f,2)-pow(d,2),0.5).real
                    if int(h1)==h1 and h1>0 and h1==h2 :
                        #print(a+b+e+f,a,b,e,f,h1)
                        for j in range(len(Hip)):
                            hip=Hip[j]
                            if h1<hip:
                                diff=pow(hip,2)-pow(h1,2)
                                c1=pow(diff,0.5).real
                                if int(c1)==c1:
                                    satisfier[counter][0],satisfier[counter][1]=c1,h1
                                    circumference.update({h1:a+b+e+f})
                                    counter+=1
    
        #This part is for the other circumference
        for i in range(counter):
            for j in range(counter):
                c,d=satisfier[i][0],satisfier[j][0]
                if c>d and satisfier[i][1]==satisfier[j][1]:
                    aminuse=d-c
                    for k in range(len(Hip)):
                        cplusd=c+d
                        hip=Hip[k]
                        if cplusd<hip:
                            a=aminuse+hip
                            #print(a,c1,d1,hip,cplusd,satisfier[j][1])
                        
                            if (pow(hip,2)-pow(cplusd,2))==pow(a,2):
                                h=satisfier[j][1]
                                hsquare=pow(h,2)
                                b,f=pow(pow(c,2)+hsquare,0.5).real,pow(pow(d,2)+hsquare,0.5).real
                                print("CIRC1:",a+b+e+f,"CIRC2:",circumference[h])
                        
    FindTwoCircumferences()
    time2=int(round(time.time()*1000))
    print(time2)
    print("s",time2-time1)
    

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: