I have total partitions of an integer and I want only those partitions which have all the values unequal. For ex.-Partitions of 3 are {1,1,1,1},{2,2},{3,1},{1,1,2} and {4}. So, the required unequal partitions are {3,1} and {4} because they contain no equal elements. The code that I have used for finding all partitions is provided below. I can filter the partitions to get the desired result, but I want some efficient way to find all the partitions, which have no equal terms in them, without finding all the partitions. I have searched the net and stackoverflow but nothing states exactly the problem that I am facing. Every idea is appreciated. Thanks.
function total_partitions_of_a_number($n) {# base case of recursion: zero is the sum of the empty list
if(!$n) return array(array()); # return empty array
# modify partitions of n-1 to form partitions of n
foreach(total_partitions_of_a_number($n-1) as $p) { # recursive call
$a[] = array_merge(array(1), $p); # "yield" array [1, p...]
if($p && (count($p) < 2 || $p[1] > $p[0])) { # p not empty, and length < 2 or p[1] > p[0]
++$p[0]; # increment first item of p
$a[] = $p; # "yield" p
}
}
return $a; # return all "yielded" values at once
}