Skip to content

Instantly share code, notes, and snippets.

@XerxesZorgon
Last active Dec 30, 2020
Embed
What would you like to do?
Additive partitions in PARI/GP
/*
Additive 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 partitions satisfying input parameters
Dependencies:
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 kFilt
\r unique
sumPart(t,c,n,u=1,e=[]) =
{
\\ List of all additive partitions
v = partitions(t);
np = numbpart(t);
\\ Select partitions meeting input requirements
P = [];
for(k = 1,np,
if(kFilt(v[k],c,n,u,e),P = concat(P,[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