Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multiplicative partition helper function for KenKen puzzles in PARI/GP
/*
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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment