Optimisation 3 array and foreach - php

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 ?

Related

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);

for loop gets executed just once

I have created an array dynamically like this way
$names = array();
for ($i = 0; $i < 100; $i++) {
$names[] = $i;
}
then created part
$parts = count($names) / 20;
and created a sub array then loop through the parts
$j = 0;
for ($i = 0; $i < $parts; $i++) {
echo "Part" . $i."<br>";
$newarray = array_slice($names, $j, 20);
for ($i = 0; $i < count($newarray); $i++) {
echo $i;
}
$j = $j + 20;
}
The problem is that this code displays from zero to 19 It doesn't display the other parts
Both the inner and outer loops use the same control variable $i, so just change the inner one...
$j = 0;
for ($i = 0; $i < $parts; $i++) {
echo "Part" . $i."<br>";
$newarray = array_slice($names, $j, 20);
for ($i1 = 0; $i1 < count($newarray); $i1++) {
echo $i1;
}
$j = $j + 20;
}

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

what i use in place of indexOf in php

here's my code so please tell me what i used in place of indexOf usind php because there is no indexOf function in php .
function generate($arrLength)
{
$arr = array();
$n = 0;
$start = 10;
$end = 20;
for($i=0; $i < $arrLength; $i++)
{
do{
$n = $start + round(rand()*($end - $start));
}while($arr.indexOf($n) !== -1);
$arr[$i] = $n;
}
return $arr;
}
$generatedArr = generate(4);
You can use array_search() in this case:
function generate($arrLength) {
$arr = array();
$n = 0;
$start = 10;
$end = 20;
for($i=0; $i < $arrLength; $i++) {
do {
$n = $start + round(rand()*($end - $start));
} while(array_search($n, $arr) !== false);
$arr[$i] = $n;
}
return $arr;
}
$generatedArr = generate(4);
echo '<pre>';
print_r($generatedArr);

Need an array of Permutation list of numbers in 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;
}

Categories