Post Reply 
July 2018 little math problem
07-27-2018, 03:37 PM (This post was last modified: 07-27-2018 03:41 PM by Albert Chan.)
Post: #19
RE: July 2018 little math problem
Discover a way to add Complement Symmetry to solve zigzag puzzles.
Removing center solutions complement equivalent is hard, so the trick is ... not remove it !

This Picat program solve all zigzag puzzles, with sides > 2:
Each solution actually represent 8, by head swap, tail swap, or reversing digits.

Code:
import cp.                  % Usage: picat zigzag.pi [Sides = 4]
main => main(4).            % Default: 4-sided zigzag
main([Sides]) => main(to_integer(Sides)).
main( Sides ) => Sides > 2, (zigzag(Sides), fail; true).

zigzag(Sides) =>
  N = 2*Sides + 1,          % number of numbers (always Odd)
  A = new_list(N),          % unknown variables
  A :: 1..N,
  all_different(A),
 
  A[1] #< A[2],             % 2X solutions by swap head pairs
  A[N-1] #< A[N],           % 2X solutions by swap tail pairs
  A[3] #< A[N-2],           % 2X solutions by flip the zigzag
  
  C = N + 1,                % C-complement Symmetry
  Center = C * 3 // 2,      % C-Complement Sum Center
  S #<= Center,             % Symmetry with slight over-count
  
  Total = N*(N+1) // 2,     % Speed-up by tighter constraint on S
  Total + sum([A[I]: I in 3..2..N-2]) #= S * Sides,

  foreach(I in 1..2..N-2)   % All sides sum to S
    A[I] + A[I+1] + A[I+2] #= S
  end,
  
  solve(A),
  writeln(A),
  
  % Add C-Complement Symmetry Solution
  S < Center, writeln([C-I: I in A]).

For a 4-sided zigzag: I still got 12, but Picat really solved only 6:
Complement Symmetry is internalized for a nice speedup.

[1,9,4,8,2,7,5,3,6]
[9,1,6,2,8,3,5,7,4]
[2,8,4,9,1,7,6,3,5]
[8,2,6,1,9,3,4,7,5]
[3,9,1,8,4,7,2,5,6]
[7,1,9,2,6,3,8,5,4]
[4,8,2,9,3,5,6,1,7]
[6,2,8,1,7,5,4,9,3]
[5,8,1,6,7,4,3,2,9]
[5,2,9,4,3,6,7,8,1]
[6,7,1,5,8,4,2,3,9]
[4,3,9,5,2,6,8,7,1]

I tried zigzag program with other sides:

Code:
Sides Time(s) solns x 8
3     0.12    56
4     0.12    96
5     0.12    480
6     0.18    1888
7     0.60    10688
8     3.71    46816
9     30.2    267616
10    263.    1502016
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
July 2018 little math problem - pier4r - 07-25-2018, 08:52 PM
RE: July 2018 little math problem - DavidM - 07-26-2018, 04:03 AM
RE: July 2018 little math problem - DavidM - 07-26-2018, 03:38 PM
RE: July 2018 little math problem - pier4r - 07-26-2018, 12:36 PM
RE: July 2018 little math problem - pier4r - 07-27-2018, 10:03 AM
RE: July 2018 little math problem - DavidM - 07-28-2018, 04:22 PM
RE: July 2018 little math problem - Albert Chan - 07-27-2018 03:37 PM
RE: July 2018 little math problem - pier4r - 08-01-2018, 02:13 PM



User(s) browsing this thread: 1 Guest(s)