Instantly share code, notes, and snippets.

# XerxesZorgon/multPart.gp

Created December 30, 2020 22:23
Show Gist options
• Save XerxesZorgon/42efdcda67fecf266d03534e50d9098c to your computer and use it in GitHub Desktop.
Multiplicative partition helper function for KenKen puzzles in PARI/GP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 /* Multiplicative partitions of integers Inputs: t: Integer (Target number) c: Number of elements in the sum (Size of the cage) n: Maximum integer in the sum partition (Size of the KenKen puzzle) u: Solutions must contain unique entries if set (Set u=0 if cage spans multiple rows or columns) e: Exclusion set (numbers will not appear in output list) Output P: List of multiplicative partitions satisfying input parameters Dependencies mpartitions.gp mnumbpart.gp kFilt.gp unique.gp, isunique.gp The number of elements in the sum c is determined by the size of the cage, while n is the size of the puzzle. If the cage is contained in a single row or column, all of the returned values must be unique, so set u to true (1). Lastly, if some entries on the same row or column of the puzzle are already known, they need to be excluded with the exclusion vector e. Written by: John Peach 28-Dec-2020 */ \\ Load dependencies \r mpartitions \r mnumbpart \r kFilt \r unique multPart(t,c,n,u=1,e=[]) = { \\ List of all additive partitions v = mpartitions(t); np = mnumbpart(t); \\ Select partitions meeting input requirements P = []; for(k = 1,np, \\ Select all solutions with cage length c if(kFilt(v[k],c,n,u,e),P = concat(P,[v[k]])); \\ Select solutions with cage length c-1 and include one 1 if(kFilt(v[k],c-1,n,u,e),P = concat(P,[concat(1,v[k])])); \\ If uniqueness is not required, allow two 1's if( u == 0, if(kFilt(v[k],c-2,n,u,e),P = concat(P,[concat([1,1],v[k])])); ); ); \\ Print partitions for(k = 1,length(P), print(Vec(P[k])) ); return(P) }