Multiple comparisons inside for loops don't break php code. Why? - php

Why this piece of code works when it is clearly wrong in the second for loop (for ($i==0; $i<$parts; $i++) {)?
Does php allows for multiple comparisons inside for loops?
function split_integer ($num,$parts) {
$value = 0;
$i = 0;
$result = [];
$modulus = $num%$parts;
if ($modulus == 0) {
for($i = 0; $i < $parts; $i++)
{
$value = $num/$parts;
$result[] = $value;
}
} else {
$valueMod = $parts - ($num % $parts);
$value = $num/$parts;
for ($i==0; $i<$parts; $i++) {
if ($i >= $valueMod) {
$result[] = floor($value+1);
} else {
$result[] = floor($value);
}
}
}
return $result;
}

Code for ($i==0; $i < $parts; $i++) runs because $i==0 has no impact on loop.
In normal for loop first statement just sets $i or any other counter's initial value. As you already set $i to 0 earlier, your loop runs from $i = 0 until second statement $i < $parts is not true.
Going further, you can even omit first statement:
$i = 0;
for (; $i < 3; $i++) {
echo $i;
}
And loop will still run 3 times from 0 to 2.

Related

Efficient loops in PHP [duplicate]

This question already has answers here:
FOR loop performance in PHP
(6 answers)
Closed 8 years ago.
What is more efficient for the following loop operation in PHP:
$k = count($array);
for ($i = 0; $i < $k; $i++)
{
echo $array[$i];
}
Or
for ($i = 0; $i < count($array); $i++)
{
echo $array[$i];
}
I know that in JavaScript there is something like this possible and more efficient:
for (i = 0, j = array.length; i < j; i++)
{
console.log(array[i]);
}
This one is more efficient than 2nd one
<?php
$k = count($array);
for($i = 0; $i < $k; $i++)
{
echo $array[$i];
}
?>
thanks.
In this specific case your first example is faster, simply because to evaluate the condition $i < count($array) PHP will execute count() after every loop. So following that logic it would be faster to use a variable in your condition, rather than a function.
The more practical approach would be to use foreach() as mentioned elsewhere.
The first one is more efficient, the second one is calling the function count() each iteration.
I would use a foreach in this context.
From the PHP Docs..
The foreach construct provides an easy way to iterate over arrays.
Why foreach is efficient in this case ?
Definitely faster since you won't be calling a count() for determining the length of the array as you did above.
You can even access the keys of the arrays.
Lets test and see.
$array = array();
$array = array_pad($array, 1000, 0);
$iters = 1e4;
$tic = microtime(true);
for ($jj = 0; $jj < $iters; $jj++) {
$k = count($array);
for($i = 0; $i < $k; $i++) {
$vv = $array[$i];
}
}
$toc = microtime(true);
$time = $toc - $tic;
echo "Time for external \$k..: $time\n";
$tic = microtime(true);
for ($jj = 0; $jj < $iters; $jj++) {
for($i = 0; $i < count($array); $i++) {
$vv = $array[$i];
}
}
$toc = microtime(true);
$time = $toc - $tic;
echo "Time for count(\$k)....: $time\n";
$tic = microtime(true);
for ($jj = 0; $jj < $iters; $jj++) {
foreach ($array as $i => $v) {
$vv = $array[$i];
}
}
$toc = microtime(true);
$time = $toc - $tic;
echo "Time foreach..........: $time\n";
Results:
Time for external $k..: 5.2356481552124
Time for count($k)....: 35.91916513443
Time foreach..........: 4.0548861026764
I think the following would be more efficient
for ($i = 0; $i < count($array); $i++)
{
echo $array[$i];
}
as no extra variable is defined to store the count of array requiring less memory space
<?php
foreach ($yourArayValue as $test=>$value)
{
echo $test;
}
?>
foreach() is batter

I need to find all amicable numbers up to a certain number

Here is my code:
$n = 300;
$set = 0;
$set2 = 0;
for($i = 1; $i<$n; $i++)
{
for($j = 1; $j <$i; $j++)
{
$qol = $i % $j;
if($qol == 0)
{
$set += $j;
}
}
for($s=1; $s<$set; $s++)
{
$qol2 = $set % $s;
if($s == 0)
{
$set2 += $s;
}
}
if($set2 == $i)
{
echo "$set and $i are amicable numbers</br>";
}
}
I do not know what the heck the problem is!
FYI: 220 and 284 are an example of amicable numbers. The sum of the proper divisors of one number are equal to other number and vice versa (wiki).
I am having troubles following your logic. In your code how would $set2 == $i ever be true? Seems to me that $i would always be greater.
I would do it the following way:
First make a separate function that finds the sums of the proper divisors:
// Function to output sum of proper divisors of $num
function sumDiv($num) {
// Return 0 if $num is 1 or less
if ($num <= 1) {
return 0;
}
$result = 1; // All nums divide by 1
$sqrt = sqrt($num);
// Add divisors to result
for ($i = 2; $i < $sqrt; $i++) {
if ($num % $i == 0) {
$result += $i + $num / $i;
}
}
// If perfect square add squareroot to result
if (floor($sqrt) == $sqrt) {
$result += $sqrt;
}
return $result;
}
Next check each iteration for a match:
$n = 1500;
for ($i = 1; $i < $n; $i++) {
// Get sum of proper devisors of $i, and sum of div. of result.
$currentDivs = sumDiv($i);
$resultDivs = sumDiv($currentDivs);
// Check for a match with sums not equal to each other.
if ($i == $resultDivs && $currentDivs != $resultDivs) {
echo "$i and $currentDivs are amicable numbers<br>";
}
}
Here a functioning phpfiddle.
Warning: Large numbers will take very long to process!

PHP array_rand / Loop / array_push / implode behavior?

Why does this not make two arrays one within 7 numbers and one within 2 numbers in it?
It somehow combines the both into one.
When i echo $arvottuLottoRivi and $lottoLisaNumerot in my HTML page the result is:
$arvottuLottoRivi (1,2,3,4,5,6,7,8,9,10) : $lottoLisaNumerot
all the seven numbers.
I have now tried three different styles but same thing happens in all cases
// VARAIBLES
$lottoNumerot = $_POST["lottoNumerot"];
$mahdollisetNumerot = array("1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31","32","33","34","35","36","37","38","39");
$i = 0;
$l = 0;
$k = 0;
//ARRAYS
$arvottuLottoRivi = array();
$lottoLisaNumerot = array();
$tenNumbersArray = array();
//FUNCTIONS
$numeroidenRandomointi = array_rand($mahdollisetNumerot, 10);
// COUNTS ARRAY LENGHT
$lottoRivinPituus = count($numeroidenRandomointi);;
// LOOPS
foreach($numeroidenRandomointi as $randomNumero){
while($i <= $lottoRivinPituus){
$i++;
}
$randomToArray = array_push($tenNumbersArray, $randomNumero);
}
// LOOPIT
foreach($tenNumbersArray as $randomToSite){
while($l <= $lottoRivinPituus){
$l++;
}
if($l <= 7){
array_push($arvottuLottoRivi, $randomToSite);
}
}
foreach($tenNumbersArray as $randomToSiteLisanuimerot){
while($k <= $lottoRivinPituus){
$k++;
}
if($k >= 7){
array_push($lottoLisaNumerot, $randomToSiteLisanuimerot);
}
}
$arvottuLottoRivi = implode(' ', $arvottuLottoRivi);
$lottoLisaNumerot = implode(' ', $lottoLisaNumerot);
When you write:
foreach($tenNumbersArray as $randomToSiteLisanuimerot){
while($k <= $lottoRivinPituus){
$k++;
}
if($k >= 7){
array_push($lottoLisaNumerot, $randomToSiteLisanuimerot);
}
}
the while loop is equivalent to:
$k = $lottoRivinPituus + 1;
Since $lottoRivinPituus is 10, $k is always 11. Therefore, if($k >= 7) is always true, so all elements of $randomToSiteLisanuumerot are copied to $lottoLisaNumerot. Similarly, in the previous loop, the test if ($l <= 7) is always false, so nothing is copied to $arvottuLottoRivi.
I think you were trying to test the current position in the loop, not the count of all elements in the array. You can do it like this:
foreach($tenNumbersArray as $l => $randomToSite){
if($l < 7){
array_push($arvottuLottoRivi, $randomToSite);
}
}
foreach($tenNumbersArray as $k => $randomToSiteLisanuimerot){
if($k >= 7){
array_push($lottoLisaNumerot, $randomToSiteLisanuimerot);
}
}
But this wastes time iterating over elements it doesn't care about. A better way would be:
$arvotSize = min(7, $lottoRivinPituus);
for ($l = 0; $l < $arvotSize; $l++) {
array_push($arvottuLottoRivi, $tenNumbersArray[$l]);
}
for ($k = $arvotSize; $k < $lottoRivinPituus; $k++) {
array_push($lottoLisaNumerot, $tenNumbersArray[$k]);
}
I really didn't get your code.
Why don't use rand function?
$randomNumbers1 = array();
$randomNumbers2 = array();
$i = 0;
while ($i < 7) {
$aNumber = rand(1, 39);
if (!in_array($aNumber, $randomNumbers1)) {
$randomNumbers1[] = $aNumber;
$i++;
}
}
$i = 0;
while ($i < 2) {
$aNumber = rand(1, 39);
if (!in_array($aNumber, $randomNumbers2)) {
$randomNumbers2[] = $aNumber;
$i++;
}
}
And if the seconds array cannot contains any number within the first one:
$i = 0;
while ($i < 2) {
$aNumber = rand(1, 39);
if (!in_array($aNumber, $randomNumbers2) && !in_array($aNumber, $randomNumbers1)) {
$randomNumbers2[] = $aNumber;
$i++;
}
}

PHP Get Experience by Level

I have this piece of code that loops 1 through 99 and is a formula.
function getExperienceByLevel ($maxLevel)
{
$levels = array ();
$current = 0;
for ($i = 1; $i <= $maxLevel; $i++)
{
$levels[$i - 1] = floor ($current / 4);
$current += floor($i+300*pow(2, ($i/9.75)));
}
return $levels;
}
First you initiate it like so $aLevels = getExperienceByLevel(99); then to see how much EXP you need to get to level 6 you do this echo $aLevels[5]; since it's an array.
Now I'm trying to do reverse. Get Level by EXP.
function getLevelByExp($exp)
{
$myLevel = 0;
$aLevels = getExperienceByLevel(99);
for ($i = 1; $i < 100; $i++)
{
if ($exp > $aLevels[$i-1])
{
return $myLevel;
}
}
}
When called upon getLevelByExp(1124); or any number inside, it seems to return a zero. But it seems to work when you put echos inside that statement.
Like instead of return $myLevel do echo "You are up to level $i<br />"; and it will go all the way up to the current level you've gained EXP for.
But still.. doesn't work when I want to simply return a number.
This seems to work better than your function:
function getLevelByExp($exp)
{
$aLevels = getExperienceByLevel(99);
for ($i = 0; $i <= 99; ++$i)
{
//echo "cmp $exp >= aLevels[$i]={$aLevels[$i]}\n";
if ($exp <= $aLevels[$i])
return $i - 1;
}
return -1;
}
It needs improvement for the edge cases, such as when $exp is zero.
Return $i instead because it always '0'
if ($exp > $aLevels[$i-1]) {
return $i;
}
You never change $myLevel, so it will always stay at 0.
Try returning $i instead of $myLevel, as $i is actually changing:
function getLevelByExp($exp) {
$aLevels = getExperienceByLevel(99);
for ($i = 1; $i < 100; $i++) {
if ($exp > $aLevels[$i-1]) {
return $i;
}
}
}

decrese value of loop

I have this code, but i want in second loop a decrease of $p value. The first internal loop must be repeated three times, the second, two times and the last, one time. I am trying $p-- but without success.
Any idea ? thanks
$p = 3;
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < $p; $o++) {
echo "something";
$p--;
}
}
Move your $p-- to outside the inner for loop:
$p = 3;
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < $p; $o++) {
echo "something";
}
$p--;
}
Or better, just depend on the value of $i:
for ($i = 0; $i < 3; $i++) {
for ($o = 0; $o < 3 - $i; $o++) {
echo "something";
}
}
Or if you don't actually use $i:
for ($i = 2; $i >= 0; $i--) {
for ($o = 0; $o < $i; $o++) {
echo "something";
}
}
It's quite simple.
for ($i = 2; $i >= 0; $i--)
{
}
Set the starting number at the upper limit number, and then go down until equal to 0, $i minus 1;
You need to decrement $p outside the first loop

Categories