Greetings to all!
I'm currently working on a permissions system for a CMS i'm building, but have hit a slight problem... maths
Take the following list:
1
2
4
8
16
32
etc...
Yep, they're all just doubling the previous value. Easy enough!
Now what I want to do is supply a number, let's say 11. I then need to find all the numbers in that list which when added together, produce that number. So in the case of 11, this would return 1, 2 and 8 (1 + 2 + 8 = 11).
Via a bit of googling, i've got the following code:
However, it doesn't work very well... it returns
[0] => 0
[1] => 0
[2] => 1
[3] => 1
[4] => 2
[5] => 4
when it should return
[0] => 1
[1] => 4
Does anyone have any ideas on how to go about solving this problem? Any ideas, attempts etc would be appreciated!
I'm currently working on a permissions system for a CMS i'm building, but have hit a slight problem... maths

Take the following list:
1
2
4
8
16
32
etc...
Yep, they're all just doubling the previous value. Easy enough!
Now what I want to do is supply a number, let's say 11. I then need to find all the numbers in that list which when added together, produce that number. So in the case of 11, this would return 1, 2 and 8 (1 + 2 + 8 = 11).
Via a bit of googling, i've got the following code:
Code:
$mask = 5;
$return = array();
while ($mask > 0) {
for($i = 0, $n = 0; $i <= $mask; $i = 1 * pow(2, $n), $n++) {
$end = $i;
$return[] = $end;
}
$mask = $mask - $end;
}
sort($return);
[0] => 0
[1] => 0
[2] => 1
[3] => 1
[4] => 2
[5] => 4
when it should return
[0] => 1
[1] => 4
Does anyone have any ideas on how to go about solving this problem? Any ideas, attempts etc would be appreciated!
