Enigmatic Code

Programming Enigma Puzzles

Enigma 1513: Two polygons

From New Scientist #2675, 27th September 2008

I drew a regular polygon that had X sides and a second regular polygon that had Y sides. Just as my second polygon had (Y−X) more sides than my first polygon, so each internal angle of the second polygon was (Y−X) degrees greater than each internal angle of my first polygon. (X+Y) was a perfect square.

How many sides did each of my polygons have?

[enigma1513]

Advertisements

5 responses to “Enigma 1513: Two polygons

  1. Jim Randell 26 September 2012 at 8:22 am

    The following Python code runs in 32ms.

    from enigma import divisor_pairs, is_square, printf
    
    # X < Y, X + Y is a perfect square and Y - X = 360(1/X - 1/Y)
    
    # rearranging:
    # Y - X = 360(Y/XY - X/XY) = 360(Y - X)/XY
    # So: XY = 360
    
    # so look for factors of 360 that sum to a perfect square
    for (X, Y) in divisor_pairs(360):
      if not(X > 2): continue
      if not is_square(X + Y): continue
      printf("X={X} Y={Y}")
    

    Solution: The first polygon has 9 sides. The second polygon polygon has 40 sides.

    • Naim Uygun 26 September 2012 at 9:07 am
      #Answer: X=9, Y=40
      #The given relation in Enigma can be reduced to X*Y=360
      for X in range(3,360):
          for Y in range(4,360):
              if not(X<Y):continue
              if  X*Y != 360: continue
              root=(X+Y)**0.5
              if root != int(root): continue
              print(X,Y)
      
      • Jim Randell 26 September 2012 at 9:55 am

        I know it hardly matters in such a small case as this, but once you’ve determined X, you don’t need a loop to determine Y. As XY = 360 you can do it much more efficiently in one step:

        (Y, r) = divmod(X, 360)
        if r: continue
        
  2. geoffrounce 12 July 2017 at 11:55 am
    % A Solution in MiniZinc
    include "globals.mzn";
    
    % Two polygons have Y and X sides
    var 3..180: Y;
    var 3..180: X;
    var 3..180: AngleY;
    var 3..180: AngleX;
    
    constraint X != Y;
    
    set of int: sq = {n*n | n in 4..20};
    constraint (X + Y) in sq;
    
    constraint AngleY = 180 * ( 1 - 2/Y) /\ AngleX = 180 * (1 - 2/X);
    
    % Polygon angle difference (Y - X) = Polygon side difference (Y - X)
    constraint 180 * (1 - 2/Y) - 180 * (1 - 2/X) == Y - X;
    
    solve satisfy;
    
    output [ "Polygon Y sides = " ++ show(Y) ++ ", Polygon X sides = " ++ show(X)
    ++ "\n" ++ "AngleY = " ++ show(AngleY) ++ ", AngleX = " ++ show(AngleX) ];
    
    % Polygon Y sides = 40, Polygon X sides = 9
    % AngleY = 171, AngleX = 140
    % --------------------------
    % Finished in 66msec ( Runs OK in Geocode solver, but not OK in Chuffed solver)
    

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: