Need an array of Permutation list of numbers in php - php

Here I need an array of all the numbers generated by permutation and also here i am giving n and k from a html form.
function combination_number($k,$n){
$n = intval($n);
$k = intval($k);
if ($k > $n){
return 0;
} elseif ($n == $k) {
return 1;
} else {
if ($k >= $n - $k){
$l = $k+1;
for ($i = $l+1 ; $i <= $n ; $i++)
$l *= $i;
$m = 1;
for ($i = 2 ; $i <= $n-$k ; $i++)
$m *= $i;
} else {
$l = ($n-$k) + 1;
for ($i = $l+1 ; $i <= $n ; $i++)
$l *= $i;
$m = 1;
for ($i = 2 ; $i <= $k ; $i++)
$m *= $i;
}
}
return $l/$m;
}
function array_combination($le, $set){
$lk = combination_number($le, count($set));
$ret = array_fill(0, $lk, array_fill(0, $le, '') );
$temp = array();
for ($i = 0 ; $i < $le ; $i++)
$temp[$i] = $i;
$ret[0] = $temp;
for ($i = 1 ; $i < $lk ; $i++){
if ($temp[$le-1] != count($set)-1){
$temp[$le-1]++;
} else {
$od = -1;
for ($j = $le-2 ; $j >= 0 ; $j--)
if ($temp[$j]+1 != $temp[$j+1]){
$od = $j;
break;
}
if ($od == -1)
break;
$temp[$od]++;
for ($j = $od+1 ; $j < $le ; $j++)
$temp[$j] = $temp[$od]+$j-$od;
}
$ret[$i] = $temp;
}
for ($i = 0 ; $i < $lk ; $i++)
for ($j = 0 ; $j < $le ; $j++)
$ret[$i][$j] = $set[$ret[$i][$j]];
return $ret;
}
$number = $_REQUEST['number'];
for($i=0;$i<$number;$i++)
{
$arr[$i]=$i+1;
}
$k = $_REQUEST['select'];
$permutations = array_combination($k, $arr);
echo "<pre>";
print_r($permutations);
This code is working but its not giving all the numbers generated by Permutation. I need those numbers also. Please tell me how can i get those? Please help.

I'm not sure if it is the best solution, but this seems to do what you want
function array_combination($le, $set){
// Your existing code
// …
for ($i = 0 ; $i < $lk ; $i++)
for ($j = 0 ; $j < $le ; $j++)
$ret[$i][$j] = $set[$ret[$i][$j]];
// Changes start here
$desiredArray = $ret;
foreach($ret as $innerArray)
{
$desiredArray[] = array_reverse($innerArray);
}
return $desiredArray;
}

Related

Optimisation 3 array and foreach

Hi how can i optimize my code ?
$time_start = microtime(true);
$max = 2000;
$vx = [];
for ($i = 0; $i < $max; $i++) {
$vx[$i] = $i;
}
$vy = [];
for ($i = 0; $i < $max; $i++) {
$vy[$i] = $i;
}
$z = [];
$nb = 0;
foreach ($vx as $kx=>$x) {
foreach ($vy as $ky=>$y) {
$z[$x][$y] = cos($x*$x)-sin($y*-2);
$nb++;
}
}
$time_end = microtime(true);
echo ($time_end-$time_start).' microsec to process and calculate '.$nb.' z values';
And after this
$time_start = microtime(true);
$max = 2000;
$vx = [];
$vy = [];
for ($i = 0; $i < $max; $i++) {
$vx[$i] = $i;
$vy[$i] = $i;
}
$z = [];
$nb = 0;
foreach ($vx as $kx=>$x) {
foreach ($vy as $ky=>$y) {
$z[$x][$y] = cos($x*$x)-sin($y*-2);
$nb++;
}
}
$time_end = microtime(true);
echo ($time_end-$time_start).' microsec to process and calculate '.$nb.' z values';
The foreach loops are not necessary nor are the 2 arrays you build. It can all be done in 2 for loops like this
$max = 10;
$z = [];
for ($i = 0; $i < $max; $i++) {
for ($j = 0; $j < $max; $j++) {
$z[$i][$j] = cos($i*$i)-sin($j*-2);
}
}
print_r($z);
And after this
$time_start = microtime(true);
$max = 2000;
$vx = [];
$vy = [];
for ($i = 0; $i < $max; $i++) {
$vx[$i] = $i;
$vy[$i] = $i;
}
$z = [];
$nb = 0;
for ($i = 0; $i < $max; $i++) {
for ($j = 0; $j < $max; $j++) {
$z[$i][$j] = cos($i*$i)-sin($j*-2);
}
}
$time_end = microtime(true);
echo ($time_end-$time_start).' microsec to process and calculate '.$nb.' z values';```
can i optimise the double foreach ?

How can received and clear an array in PHP

I'm trying to look for a number with maximum divisors in a range of 1 - 10000.
I succeeded, but then I wish to verify if there exist more than two max divisors and print them out. My array is really the problem. How can I clear an array and assign a new integer to it in an if else if statement?
Here is what I have tried:
function countDivisors(){
$input = 10000;
$maxNumOfDiv = -1;
$intWMaxDivs = -1;
$curNumOfDiv = 0;
$arr = array();
for($i=1; $i <= $input; $i++) {
$curNumOfDiv = 0;
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0)
$curNumOfDiv++;
}
if($curNumOfDiv = $maxNumOfDiv){
$arr[] = $i;
$intWMaxDivs = $i;
$maxNumOfDiv = $curNumOfDiv;
} else if($curNumOfDiv > $maxNumOfDiv){
$arr = array();
$arr[] = $intWMaxDivs
$maxNumOfDiv = $curNumOfDiv;
}
}
for ($i; $i < count($arr); $i++){
echo $arr[$i]['intWMaxDivs'];
echo $arr[$i]['maxNumOfDiv'];
}
$div = [];
$maxDivKey = false;
$maxDiv = 0;
for($i = 1; $i <= 10000; $i++) {
for ($j = 1; $j < $i; $j++){
if ($i % $j == 0){
$div[$i][] = $i.'/'.$j.'='.$i/$j;
}
if($j == $i-1){
$count = count($div[$i]);
$div[$i]['count'] = $count;
if($maxDiv < $count){
$maxDiv = $count;
$maxDivKey = $i;
}
}
}
}
echo '<h1>Max divisors:</h1>';
print_r($div[$maxDivKey]);
//print_r($div);
I may be misunderstanding this question a little. If you are looking for a single number with maximum number of dividers, it should be something like this.
<?php
$max_num=10000;
$start_num=1;
$max_divs=-1;
$max_number=-1;
$numbers=array();
$max_divs_arr=array();
for($i=$start_num;$i<=$max_num;$i++)
{
$divs=0;
$div_array=array();
for($j=$start_num;$j<=$i;$j++)
{
if($i%$j==0)
{
$divs++;
$div_array[]=$j;
}
}
if($divs==$max_divs)
$max_divs_arr[$i]=$div_array;
if($divs>$max_divs)
{
$max_divs_arr=array();
$max_divs=$divs;
$max_divs_arr[$i]=$div_array;
}
}
foreach($max_divs_arr as $number=>$divisors)
echo "\nNumber with most divisors is $number\nIt has $max_divs divisors\nThose divisors are:".implode(',',$divisors);

Convert array outputted from DCT to an image in PHP

Using the following code I can get the DCT of an image in PHP. Then I need to convert this back in to the compressed image. How can I achieve that?
<?php
$results = array();
$image1 = "baboon.jpg";
$ima = ImageCreateFromJPEG($image1);
$N1 = imagesx($ima);
$N2 = imagesy($ima);
$rows = array();
$row = array();
for ($j = 0; $j < $N2; $j++) {
for ($i = 0; $i < $N1; $i++)
$row[$i] = imagecolorat($ima, $i, $j);
$rows[$j] = dct1D($row);
}
for ($i = 0; $i < $N1; $i++) {
for ($j = 0; $j < $N2; $j++)
$col[$j] = $rows[$j][$i];
$results[$i] = dct1D($col);
}
print_r($results);
function dct1D($in) {
$results = array();
$N = count($in);
for ($k = 0; $k < $N; $k++) {
$sum = 0;
for ($n = 0; $n < $N; $n++) {
$sum += $in[$n] * cos($k * pi() * ($n + 0.5) / ($N));
}
$sum *= sqrt(2 / $N);
if ($k == 0) {
$sum *= 1 / sqrt(2);
}
$results[$k] = $sum;
}
return $results;
}
?>
Also I need to know how can I add some extra details like another message to this image too.. (image steganography). Please help. Thanks

Gaussian Eliminate for GF2 code

I have this gauss_eliminate function, but instead of dealing with real numbers, I want it to work on binary values.
I need the GF2 gauss_eliminate function, where the input is binary and the output is binary.
This produces real values, not binary, eg
0.57142857142857
0.71428571428571
-0.42857142857143
-0.28571428571429
0.14285714285714
Gaussian elimination has these 3 allowed steps:
1) Swapping two rows (for achieving a certain look)
2) Multiplying a row by a nonzero number,
3) Adding a multiple of one row to another row.
-- in GF2: addition operation is XOR : 0+0=0, 0+1=1, 1+0=1, 1+1=0 --and--
multiplication is AND operation: 0*0=0,0*1=0,1*0=0,1*1=1
function gauss_eliminate($A, $b, $N)
{
for ($col = 0; $col < $N; $col++) {
$j = $col;
$max = $A[$j][$j];
for ($i = $col + 1; $i < $N; $i++) {
$tmp = abs($A[$i][$col]);
if ($tmp > $max) {
$j = $i;
$max = $tmp;
}
}
swap_rows($A, $b, $col, $j);
for ($i = $col + 1; $i < $N; $i++) {
$tmp = $A[$i][$col] / $A[$col][$col];
for ($j = $col + 1; $j < $N; $j++) $A[$i][$j] -= $tmp * $A[$col][$j];
$A[$i][$col] = 0;
$b[$i] -= $tmp * $b[$col];
}
}
$x = array();
for ($col = $N - 1; $col >= 0; $col--) {
$tmp = $b[$col];
for ($j = $N - 1; $j > $col; $j--) $tmp -= $x[$j] * $A[$col][$j];
$x[$col] = $tmp / $A[$col][$col];
}
return $x;
}
new code #1, still doesn't work:
function gauss_eliminate($A, $b, $N)
{
for ($col = 0; $col < $N; $col++) {
$j = $col;
$max = $A[$j][$j];
for ($i = $col + 1; $i < $N; $i++) {
$tmp = abs($A[$i][$col]);
if ($tmp > $max) {
$j = $i;
$max = $tmp;
}
}
swap_rows($A, $b, $col, $j);
for ($i = $col + 1; $i < $N; $i++) {
for ($j = $col + 1; $j < $N; $j++)
$A[$i][$j]=( $A[$i][$j] != $A[$col][$j] ) ? 1 : 0;
$A[$i][$col] = 0;
$b[$i]=( $b[$i] != $b[$col] ) ? 1 : 0;
}
}
$x = array();
for ($col = $N - 1; $col >= 0; $col--) {
# $tmp = $b[$col];
# for ($j = $N - 1; $j > $col; $j--) $tmp -= $x[$j] * $A[$col][$j];
$x[$col] = ( $x[$col] != $A[$col][$j] ) ? 1 : 0;
}
return $x;
}
New code #2 - still doesn't work - tmp setup to alternate
function gauss_eliminate($A, $b, $N)
{
for ($col = 0; $col < $N; $col++) {
$j = $col;
$max = $A[$j][$j];
for ($i = $col + 1; $i < $N; $i++) {
$tmp = abs($A[$i][$col]);
if ($tmp > $max) { $j = $i; $max = $tmp; }
}
swap_rows($A, $b, $col, $j);
for ($i = $col + 1; $i < $N; $i++) {
# $tmp = $A[$i][$col] / $A[$col][$col];
for ($j = $col + 1; $j < $N; $j++) $A[$i][$j]=($A[$i][$j] != $A[$col][$j] ? 1 : 0);
$A[$i][$col] = 0;
$b[$i] = ( $b[$i] != $b[$col] ? 1 : 0);
}
}
$x = array();
for ($col = $N - 1; $col >= 0; $col--) {
$tmp = $b[$col];
for ($j = $N - 1; $j > $col; $j--) $tmp = 1 - $tmp;
$x[$col] = ($tmp != $A[$col][$j] ? 1 : 0);
}
return $x;
}
It appears I found the right syntax. I am getting the right result for one example, after modifying the code in a way that makes sense.... converting - to +, and this + to XOR, while / is ignored and * is AND.
Still it would be nice to get a confirmation that this code is correct.
function gauss_eliminate($A, $b, $N) {
for ($col = 0; $col < $N; $col++) {
$j = $col;
$max = $A[$j][$j];
for ($i = $col + 1; $i < $N; $i++) {
$tmp = abs($A[$i][$col]);
if ($tmp > $max) {
$j = $i;
$max = $tmp;
}
}
swap_rows($A, $b, $col, $j);
for ($i = $col + 1; $i < $N; $i++) {
# $tmp = $A[$i][$col] / $A[$col][$col];
# for ($j = $col + 1; $j < $N; $j++) {
# $A[$i][$j] -= $tmp * $A[$col][$j];
# }
# $A[$i][$col] = 0;
# $b[$i] -= $tmp * $b[$col];
$tmp = $A[$i][$col];
for ($j = $col + 1; $j < $N; $j++) {
# $A[$i][$j] = $A[$i][$j] + ( $tmp * $A[$col][$j] );
$A[$i][$j] = ( $A[$i][$j] != ( $tmp && $A[$col][$j] ) ) ? 1 : 0;
}
$A[$i][$col] = 0;
# $b[$i] = $b[$i] + ($tmp * $b[$col]);
$b[$i] = ( $b[$i] != ($tmp && $b[$col]) ) ? 1 : 0;
}
}
$x = array();
for ($col = $N - 1; $col >= 0; $col--) {
$tmp = $b[$col];
for ($j = $N - 1; $j > $col; $j--) {
# $tmp -= $x[$j] * $A[$col][$j];
# $tmp = $tmp + ($x[$j] * $A[$col][$j]);
$tmp = ( $tmp != ($x[$j] && $A[$col][$j]) ) ? 1 : 0;
}
# $x[$col] = $tmp / $A[$col][$col];
$x[$col] = $tmp;
}
return $x;
}
The commented text is the old (non GF2 code) as well as my "middle step" of showing where I convert + to XOR, * to AND, etc

Array don't return expected text in PHP

Why the following code do not return me 0 => 'Zero' for the first line but 0 => 0 ?
for ($i = 0; $i <= 30; $i += 1) {
if($i == 0) { $array[$i] = 'Zero'; }
$array[$i] = $i;
}
for ($i = 30; $i <= 100; $i += 5) {
$array[$i] = $i;
}
for ($i = 100; $i <= 200; $i += 10) {
$array[$i] = $i;
}
return $array;
Thanks.
You set element 0 to Zero and then overwrite it in the next line with 0.
if($i == 0) { $array[$i] = 'Zero'; }
$array[$i] = $i;
You probably want an else...
if($i == 0) {
$array[$i] = 'Zero'; }
else {
$array[$i] = $i;
}
for ($i = 0; $i <= 30; $i += 1)
{
if($i == 0)
{
$array[$i] = 'Zero';
}
$array[$i] = $i;
}
should be:
for ($i = 0; $i <= 30; $i += 1)
{
if($i == 0)
{
$array[$i] = 'Zero';
}
else
{
$array[$i] = $i;
}
}

Categories