<?php
|
<?php
|
|
|
class VersatileCounter {
|
class VersatileCounter {
|
|
|
static $masks = array(
|
static $masks = array(
|
|
|
2 => 0x3,
|
2 => 0x3,
|
3 => 0x6,
|
3 => 0x6,
|
4 => 0xC,
|
4 => 0xC,
|
5 => 0x14,
|
5 => 0x14,
|
6 => 0x30,
|
6 => 0x30,
|
7 => 0x60,
|
7 => 0x60,
|
8 => 0xB8,
|
8 => 0xB8,
|
9 => 0x110,
|
9 => 0x110,
|
10 => 0x240,
|
10 => 0x240,
|
11 => 0x500,
|
11 => 0x500,
|
12 => 0x829,
|
12 => 0x829,
|
13 => 0x100C,
|
13 => 0x100C,
|
14 => 0x2015,
|
14 => 0x2015,
|
15 => 0x6000,
|
15 => 0x6000,
|
16 => 0xD008,
|
16 => 0xD008,
|
17 => 0x12000,
|
17 => 0x12000,
|
18 => 0x20400,
|
18 => 0x20400,
|
19 => 0x40023,
|
19 => 0x40023,
|
20 => 0x82000,
|
20 => 0x90000,
|
21 => 0x140000,
|
21 => 0x140000,
|
22 => 0x300000,
|
22 => 0x300000,
|
23 => 0x420000,
|
23 => 0x420000,
|
24 => 0xE10000,
|
24 => 0xE10000,
|
25 => 0x1200000,
|
25 => 0x1200000,
|
26 => 0x2000023,
|
26 => 0x2000023,
|
27 => 0x4000013,
|
27 => 0x4000013,
|
28 => 0xC800000,
|
28 => 0xC800000,
|
29 => 0x14000000,
|
29 => 0x14000000,
|
30 => 0x20000029,
|
30 => 0x20000029,
|
31 => 0x48000000,
|
31 => 0x48000000,
|
32 => 0x80200003
|
32 => 0x80200003
|
|
|
);
|
);
|
|
|
private $length, $mask;
|
private $length, $mask;
|
|
|
private $state = 0;
|
private $state = 0;
|
|
|
function __construct($length = 8) {
|
function __construct($length = 8) {
|
$this->length = $length;
|
$this->length = $length;
|
$this->mask = self::$masks[$length];
|
$this->mask = self::$masks[$length];
|
}
|
}
|
|
|
function state() {
|
function state() {
|
return sprintf("%0" . $this->length . "b",$this->state);
|
return sprintf("%0" . $this->length . "b",$this->state);
|
}
|
}
|
|
|
function tick() {
|
function tick() {
|
|
|
$ns = ($this->state * 2) & (pow(2,$this->length) - 1);
|
$ns = ($this->state * 2) & (pow(2,$this->length) - 1);
|
|
|
$new = $this->state & pow(2,$this->length - 1);
|
$new = $this->state & pow(2,$this->length - 1);
|
|
|
for($i = $this->length - 2;$i >= 0;$i--) {
|
for($i = $this->length - 2;$i >= 0;$i--) {
|
|
|
if($this->mask & pow(2,$i)) {
|
if($this->mask & pow(2,$i)) {
|
$new = (int) ($new xor ($this->state & pow(2,$i)));
|
$new = (int) ($new xor ($this->state & pow(2,$i)));
|
}
|
}
|
|
|
}
|
}
|
|
|
if($new == 0) {
|
if($new == 0) {
|
$ns++;
|
$ns++;
|
}
|
}
|
|
|
$this->state = $ns;
|
$this->state = $ns;
|
|
|
}
|
}
|
|
|
function run($ticks,$output = false) {
|
function run($ticks,$output = false) {
|
|
|
if($output) {
|
if($output) {
|
printf("%" . strlen($ticks) . "u: %0" . $this->length . "b\n",0,$this->state);
|
printf("%" . strlen($ticks) . "u: %0" . $this->length . "b\n",0,$this->state);
|
}
|
}
|
|
|
for($i=0;$i<$ticks;$i++) {
|
for($i=0;$i<$ticks;$i++) {
|
$this->tick();
|
$this->tick();
|
if($output) {
|
if($output) {
|
printf("%" . strlen($ticks) . "u: %0" . $this->length . "b\n",$i+1,$this->state);
|
printf("%" . strlen($ticks) . "u: %0" . $this->length . "b\n",$i+1,$this->state);
|
}
|
}
|
}
|
}
|
|
|
}
|
}
|
|
|
static function GetState($length,$ticks) {
|
static function GetState($length,$ticks) {
|
|
|
$VC = new self($length);
|
$VC = new self($length);
|
$VC->run($ticks);
|
$VC->run($ticks);
|
|
|
return $VC->state;
|
return $VC->state;
|
|
|
}
|
}
|
|
|
}
|
}
|
|
|
|
|
|
|
|
|