I am trying to have an array with n elements permute through like so:
permute(x,y,z)
permute(-x,y,z)
permute(x,-y,z)
permute(-x,-y,z)
It is like incrementing in binary (if the – signs represented 1s).
I am attempting to do that in code, and have noticed this:
list[1] = -list[1];
perm(list, k, m);
list[1] = -list[1];
list[2] = -list[2];
perm(list, k, m);
list[1] = -list[1];
perm(list, k, m);
list[1] = -list[1];
list[2] = -list[2];
list[3] = -list[3];
perm(list, k, m);
list[1] = -list[1];
perm(list, k, m);
list[1] = -list[1];
list[2] = -list[2];
perm(list, k, m);
list[1] = -list[1];
perm(list, k, m);
list[1] = -list[1];
list[2] = -list[2];
list[3] = -list[3];
list[4] = -list[4];
perm(list, k, m);
I have noticed that some sections repeat. Is there a way to write this into a loop? Thanks.
This returns the kth permutation according to your scheme:
Example:
Output: