N-uniquely additive sets
Jelly, 7 bytes
œcS€ṢIP
Try it online!
Returns a positive number for truthy and zero for falsey.
œc find combinations
S€ sum each combination
Ṣ sort the sums
I find the difference between each pair of sums
iff any sums are the same, this returns a list containing 0
P product of the elements of the resulting list
Matlab, 78 bytes
function n=f(s,n);p=perms(s);k=sum(unique(sort(p(:,1:n)),'rows')');unique(k)-k
This function returns a positive value (in fact n
) for truthy and returns an error as a falsey answer (valid according to this comment)
Explanation:
function n=f(s,n);
p=perms(s); %create all permutations of the set
k=sum(unique(sort(p(:,1:n)),'rows')');
%just take the first n entries of each permutation
%sort those entries and
%filter out all duplicates (we sorted as the order should NOT matter)
%then sum each of those candidates
unique(k)-k
%if all those sums are distinct, unique(k) will have the same size
% as k itself, and therefore we can subtract, otherwise it will throw
% an error as we try to subtract vectors of different sizes
MATL, 7 bytes
XN!sSdA
Try it online!
Returns true
(displayed as 1
) or false
(displayed as 0
).
XN % Take array S and number N. Generate all combinations of elements from S
% taken N at a time. Gives a 2D array where each combination is a row
! % Transpose. Each combination is now a column
s % Sum of each column: gives a row array. If N=1 computes the sum of
% the only row, and so gives a number
S % Sort vector
d % Array of consecutive differences. For a single number gives an empty array
A % True if all elements of the input array are nonzero (for an empty array
% it also gives true)