I am working on some algorithm in PHP dealing with binary values or strings containing 0s and 1s. I am calculating the list for n-numbers, from starting list 0f {0,1} for n=1; Anyway, arrays a[] and b[] are becoming big after for n > 20, reaching memory issue. So my question is how this algorithm can be optimized to use less memory? Should i store binary strings in a different format in memory except string format or do I need to restructure the algorithm itself? Any idea?
while ($n < 1 || $n > 65)
fscanf(STDIN, "%d\n", $n);
$listn = array("0","1");
$doublearray[] = $listn;
for ($i=1; $i<$n;$i++) {
foreach ($listn as $member) {
$a[] = "0" . $member;
}
$reflectedlistn = array_reverse($listn);
foreach ($reflectedlistn as $member) {
$b[] = "1" . $member;
}
$listn = array_merge($a, $b);
$doublearray[] = $listn;
$a = array();
$b = array();
}
$arr = array_slice($doublearray[$n-1], -$n);
echo "\n";
foreach ($arr as $item) {
echo $item . "\n";
}