Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Lock
- {
- public $n;
- public $attempts = 1;
- public $combinations = [];
- function __construct($n = 1) {
- $this->n = $n;
- $c = 0;
- for($row = 0; $row < 2**$n; $row++) {
- $r = [];
- $sum = 0;
- for ($col = 0; $col < $n; $col++) {
- $r[$col] = ($c >> ($col)) % 2;
- $sum += $r[$col];
- }
- $this->combinations[$sum][] = $r;
- $c++;
- }
- $max = 0;
- for($row = 0; $row < $n; $row++) {
- $len = count($this->combinations[$row]);
- if($len > $max)
- $max = $len;
- }
- $this->attempts = $max;
- }
- }
- ......
- for ($n = 2; $n <= 10; $n++){
- $lock = new Lock($n);
- dump($lock);
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement