July 2018 little math problem
08-03-2018, 05:25 PM (This post was last modified: 05-19-2019 11:31 PM by Albert Chan.)
Post: #30
 Albert Chan Senior Member Posts: 2,556 Joined: Jul 2018
RE: July 2018 little math problem
I revised zigzag.pi with better constraints

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),   M = [N-1,N] ++ (1..N-2),  % head moved to back for speedup   A[M[1]] #< A[M[2]],       % 2X solution by swap head pairs   A[M[N-1]] #< A[M[N]],     % 2X solution by swap tail pairs   A[M[3]] #< A[M[N-2]],     % 2X solution 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 with non-vertices constraint   K = Sides//2 + 1,         % Remove as much vertices as possible   (odd(Sides) ->            % For odd-Sided zigzag, no vertices left    sum([A[M[I]]: I in N-3..-4..4]) #= Total - K*S ;    sum([A[M[I]]: I in N-3..-4..4]) #= Total - K*S + A[M[3]]   ),   foreach(I in 1..2..N-4)   % All sides sum to S (tail side skipped)     A[M[I]] + A[M[I+1]] + A[M[I+2]] #= S   end,   solve(A),   writeln([A[I]: I in M]),   % Add C-Complement Symmetry Solution   S < Center, writeln([C-A[I]: I in M]).

For 10-sided zigzag, speed (vs. original) = 2.37X, or 137% faster (finished in 111 seconds)
 « Next Oldest | Next Newest »

 Messages In This Thread July 2018 little math problem - pier4r - 07-25-2018, 08:52 PM RE: July 2018 little math problem - Voldemar - 07-25-2018, 09:33 PM RE: July 2018 little math problem - Thomas Klemm - 07-25-2018, 10:27 PM RE: July 2018 little math problem - Albert Chan - 07-26-2018, 12:16 AM RE: July 2018 little math problem - John Keith - 07-26-2018, 12:57 AM RE: July 2018 little math problem - DavidM - 07-26-2018, 04:03 AM RE: July 2018 little math problem - John Keith - 07-26-2018, 01:03 PM RE: July 2018 little math problem - DavidM - 07-26-2018, 03:38 PM RE: July 2018 little math problem - Albert Chan - 07-26-2018, 02:39 AM RE: July 2018 little math problem - Thomas Klemm - 07-26-2018, 03:42 AM RE: July 2018 little math problem - Albert Chan - 07-26-2018, 06:16 PM RE: July 2018 little math problem - pier4r - 07-26-2018, 12:36 PM RE: July 2018 little math problem - Thomas Klemm - 07-26-2018, 03:43 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-29-2018, 12:10 AM RE: July 2018 little math problem - Albert Chan - 07-26-2018, 10:45 PM RE: July 2018 little math problem - Valentin Albillo - 07-27-2018, 01:30 AM RE: July 2018 little math problem - Albert Chan - 07-27-2018, 03:06 AM RE: July 2018 little math problem - cyrille de brĂ©bisson - 07-27-2018, 08:20 AM RE: July 2018 little math problem - Albert Chan - 07-27-2018, 03:37 PM RE: July 2018 little math problem - Albert Chan - 07-27-2018, 09:50 PM RE: July 2018 little math problem, mini-challenge - Albert Chan - 07-28-2018, 01:15 PM RE: July 2018 little math problem - Albert Chan - 07-29-2018, 01:02 PM RE: July 2018 little math problem - Albert Chan - 07-29-2018, 10:27 PM RE: July 2018 little math problem - Komanguy - 07-31-2018, 08:19 PM RE: July 2018 little math problem - Albert Chan - 07-31-2018, 10:49 PM RE: July 2018 little math problem - Komanguy - 08-01-2018, 11:31 AM RE: July 2018 little math problem - pier4r - 08-01-2018, 02:13 PM RE: July 2018 little math problem - Albert Chan - 08-03-2018 05:25 PM RE: July 2018 little math problem - Dave Britten - 08-13-2018, 01:54 AM

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