How can received and clear an array in PHP - 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);

Related

Check If N == M*2 in the array

I'm starting solving problem on leetcode, this problem didn't pass the test cases, this my try:
function checkIfExist($arr) {
$i = 0;
$j = 0;
$n = count($arr);
// loop over the array
for($i; $i < $n; $i++) {
for($j; $j < $n; $j++) {
// check if element of i and j not the same and N*2 = M
if ($i != $j && $arr[$i] * 2 == $arr[$j]) {
return true;
}
}
}
return false;
}
Can please explain to me where I did the error here?
This should work, try this though (it's like one of the sort algorithms). That is weird because only difference is the initialization of the $i and $j
function checkIfExist($arr) {
$n = count($arr);
// loop over the array
for($i = 0; $i < $n - 1; $i++) {
for($j = $i + 1; $j < $n; $j++) {
// check if element of i and j not the same and N*2 = M
if ($i != $j && $arr[$i] * 2 == $arr[$j]) {
return true;
}
}
}
return false;
}
The initializing of $j and $i pointer inside the for-loop done the work
function checkIfExist($arr) {
$n = count($arr);
// loop over the array
for($i = 0; $i < $n; $i++) {
for($j = 0; $j < $n; $j++) {
// check if element of i and j not the same and N*2 = M
if ($i != $j && $arr[$i] * 2 == $arr[$j]) {
return true;
}
}
}
return false;
}

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

how to generate dynamic number array like below

$big_box = array(1,2,3,16,17,18,31,32,33,46,47,48,61,62,63,76,77,78,91,92,93,106,107,108,121,122,123,136,137,138............);
$small_box = array(4,19,34,49,64.............);
I want to generate dynamic numbers array like above example upto 10000.
For Array $big_box Try to something like this.
$j = 0;
for ($i=0; $i <= 1000; $i++) {
if ($i!=0) {
$big_box[] = $i;
$j++;
}
if ($j == 3) {
$j = 1;
$big_box[] = $i+13;
$i =$i+13;
}
}
echo "<pre>";
print_r($big_box);
And array $small_box
$small_box = array();
for($i=0; $i <= 1000; $i++) {
if ($i == 0) {
$small_box[$i] = 4;
}else{
$small_box[$i] = $small_box[$i-1]+15;
}
}
echo "<pre>";
print_r($small_box);

How to count how many duplicates an array has in Php

I'm new to Php and today I came across the rand()-function. I'd like to fill an array with numbers created with this function and then count the number of its duplicates. I already tried it a first time, but somehow I seem to be on the woodway.
<?php
$numbers = array();
for ($i=0; $i < 100; $i++) {
$numbers[$i] = rand(0, 100);
}
//$numbers = array(12,12,12,12);
echo "random numbers generated.<br>";
$arrLength = count($numbers);
$arrWithDoubles = array();
for ($i=0; $i < $arrLength; $i++) {
//echo "start looping for i: ".$i."! numbers['i'] has the content".$numbers[$i].".<br>";
for ($x=$i; $x < $arrLength; $x++) {
//echo "looped for x: ".$x."! numbers['x'] has the content".$numbers[$x].".<br>";
if($numbers[$i] == $numbers[$x]) {
if($i != $x) {
//echo "pushed to 'arrWithDoubles'.<br>";
array_push($arrWithDoubles, $numbers[$x]);
}
}
}
}
echo "numbers of doubles: ".count($arrWithDoubles)."<br>";
echo "list of numbers which were double:<br>";
for ($i=0; $i < count($arrWithDoubles); $i++) {
echo $arrWithDoubles[$i];
echo "<br>";
}
?>
The array_unique() function removes duplicates from an array, then just add a bit of math.
<?php
$numberOfDuplicates = count($orginalArray) - (count($orginalArray) - count(array_unique($originalArray)));
?>
$origin = array(2,4,5,4,6,2);
$count_origin = count($origin);
$unique = array_unique($origin);
$count_unique = count($unique);
$duplicates = $count_origin - $count_unique;
echo $duplicates;
$count = array();
foreach ($srcRandom as $sr) {
if (!array_key_exists ($sr, $count) ) {
$count[$sr] = 1;
continue;
}
$count[$sr]++;
}
var_dump ($count);
Thanks for all your input. With that I came to the following solution which fits my demand the best:
<?php
function countValueInArray($value, $array) {
$count = 0;
for ($i=0; $i < count($array); $i++) {
if($value == $array[$i]) {
$count++;
}
}
return $count;
}
$numbers = array();
for ($i=0; $i < 100; $i++) {
$numbers[$i] = rand(0, 100);
}
$duplicates = array();
for ($x=0; $x < count($numbers); $x++) {
$number = countValueInArray($numbers[$x], $numbers);
if ($number > 1) {
array_push($duplicates, $numbers[$x]);
}
}
$duplicatesList = array_values(array_unique($duplicates));
echo "number of duplicates: ".count($duplicatesList);
echo "<br>these are: <br>";
print_r($duplicatesList);
?>
Thanks a lot for your help!

Refactoring 2 similar if statements

Would you write this code any differently? I don't mind tertiary operator by the way so any ideas that include it are also welcome
if ($stopYear < $startYear) {
for ($i = $startYear; $i >= $stopYear; $i--) {
$yearMultiOptions[$i] = $i;
}
} else {
for ($i = $startYear; $i <= $stopYear; $i++) {
$yearMultiOptions[$i] = $i;
}
}
$min = min($startYear, $stopYear);
$max = max($startYear, $stopYear);
for ($i = $min; $i <= $max; $i++) {
$yearMultiOptions[$i] = $i;
}
I don't know php, so min and max might have different syntax, but you get the idea.
You could do something like this:
$step = ($stopYear < $startYear) ? -1 : 1;
for ($i = $startYear; $i != $stopYear; $i += step) {
$yearMultiOptions[$i] = $i;
}

Categories