Php for loop issue - php

Hi I'm Learning php and trying to do a for loop look like this wtih this code:
for($x=1; $x<=20; $x++){
echo $x;
$x = $x + 3; //5
echo "<br/>";
}
It's produce
1
5
9
13
14
But I want it should be...
1
5
10
15
20

for ($x = 0; $x <= 20; $x += 5) {
echo ($x == 0 ? 1 : $x), '<br>';
}
Or:
foreach (range(0, 20, 5) as $x) {
echo ($x == 0 ? 1 : $x), '<br>';
}
You can't produce the sequence without 1 extra condition because the delta differs in the first step:
1 + 4 ...
5 + 5
10 + 5
15 + 5
20 + 5

there are more than one solution.
one is:
for($x=1; $x<=20; $x++){
if(!($x % 5) || $x==1)
echo $x . "<br />";
}
Explination
% is the modulo operator. It returns the devision rest.
lets say $x is 3 than 3 % 5 would return 3 because the result 3/5 = 0 rest 3
if its $x is 10, it return 0. 10/5 = 2 rest 0
In the if-statement I use !-not operator. This turns around the result.
Because if takes 1+ (one and more) as true and 0- (zero and less) as false
So rest of 3 would be positiv (true) but in this case i want it to be false. So I turn arount the true/false with !
% - Modulo
R - Rest
1 % 5 = 0 R 1 // would say true to if
2 % 5 = 0 R 2 // would say true to if
3 % 5 = 0 R 3 // would say true to if
4 % 5 = 0 R 4 // would say true to if
5 % 5 = 1 R 0 // would say false to if
6 % 5 = 1 R 1 // would say true to if
and so on...

try this.
$x = 1;
for ($j = 1; $j <= 20; $j++) {
echo $x, "<br/>";
if ($x == 1) {
$x = $x + 4;
} else {
$x = $x + 5;
if($x > 20){break;}
}
}
if this answer is work for you please mark the answer.

Try this:
for($x=1; $x<=20; $x++) {
if($x%5==0 || $x==1) {
echo $x;
echo "<br>";
}
}

Related

Test whenever a value x is greater by exactly 1 compared to a value y modulo 3

How do I check that x is 1 greater (ahead) of y in a modulo sense (mod 3)?
This is when x = 0 and y = 2, when x = 1, y = 0 and when x = 2, y = 1.
I tried testing like this:
php -a
php > $x = 0;
php > $y = 2;
php > echo ($x - $y) % 3;
-2
php > $x = 1;
php > $y = 0;
php > echo ($x - $y) % 3;
1
php > $x = 2;
php > $y = 1;
php > echo ($x - $y) % 3;
1
It is not working for the case where x = 0 and y = 2. How can I calculate this so that $x is 'ahead' of $y by 1 in a modulo sense?
I will first explain my understanding of your following sentence:
How do I check that x is 1 greater (ahead) of y in a modulo sense (mod 3)?
Following the examples provided, I assume you mean that, if 1 is added to $y, and we take the mod 3 of $y, we would get the mod 3 of $x.
With that in mind, we could write the following code, witch would return true if $x is "ahead" of $y by 1. (I hope you can abstract that example to whatever situation you are facing):
function check($x, $y, $mod) {
return $x % $mod == ($y + 1) % $mod;
}
//$x = 0 and $y = 2
echo check(0,2,3); //returns true
//$x = 1 and $y = 0
echo check(1,0,3); //returns true
//$x = 2 and $y = 1
echo check(2,1,3); //returns true
//$x = 0 and $y = 1
echo check(0,1,3); //returns false because $x is 2 "ahead" of $y
If you want a more generalized version of the function, with an arbitrary difference, you can use this (it should work with positive and negative differences):
function check($x, $y, $mod, $diff) {
return $x % $mod == ($y + $diff) % $mod;
}
Why is it that you used 0 for x and 2 for y? (Sorry, but I'm not much of an algebraist.)
Although your situations are pretty much clarified, it doesn't make sense (at least for me) that x would be 1 bit ahead of y when it is clearly 0 < 2.
As we know, the statement (0 - 2) % 3 would be -2 because the mod is 3, and 0 - 2 is -2, so the result is -2.
Mathematics can have some illogical sense (in my opinion) sometimes, so its worth noting that 0 isn't ahead of 2 (in the sense of programming variables) at all.
And to actually answer your question on calculation, you could have x = 0 and y = -1 as your variables instead, as per se the logic of your statement, then just increment by 1 for both variables, and the result is still the same.
Proof:
<?php
$x = 0
$y = -1
echo ($x - $y) % 3 // outputs 1
echo (++$x - ++$y) % 3 // still outputs 1
echo (++$x - ++$y) % 3 // also outputs 1
echo (++$x - ++$y) % 3 // yep, still the same
echo (++$x - ++$y) % 3 // alright, you get the deal
?>

Pascals Diagonal Triangle in PHP

I'm trying to use php code to print out pascals triangle (in the diagonal style like this- http://www.cut-the-knot.org/arithmetic/combinatorics/PascalTriangle.gif)
I tried this code:
<?php
$f = 10;
for ($x = 0; $x <= $f; $x++) {
echo "1"." ";
$previous_line[$x]=1;
}
echo "<br>";
for ($x = 0; $x < $f; $x++) {
echo "1"." ";
for ($y = 1; $y <= $f-$x-1; $y++) {
$sum = 0;
for ($z = 0; $z <= $y; $z++) {
$sum = $sum + $previous_line[$z];
}
echo $sum." ";
}
echo "<br>";
}
But I get this output:
1 1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
What am I doing wrong?
I think you use same $previous_line[$] value for every line, so the the iteration value of $sum will increased constantly. (increased by 1)
You should update $previous_line[$] value on every line:
$previous_line[$y] = $sum;
and you don't need to use this iteration:
for ($z = 0; $z <= $y; $z++) {....}
This is full code:
<?php
$f = 10;
for ($x = 0; $x <= $f; $x++) {
echo "1"." ";
$previous_line[$x]=1;
}
echo "<br>";
for ($x = 0; $x < $f; $x++) {
$sum = 1;
echo $sum." ";
for ($y = 1; $y <= $f-$x-1; $y++) {
$sum = $sum + $previous_line[$y];
echo $sum." ";
$previous_line[$y] = $sum;
}
echo "<br>";
}
Just try it
Since Justin beat me to the punch, I though I would post an improved version.
Please note that there is probably better ways to do this.
I removed your first loop since it wasn't need, then I moved $previous_line inside the second loop and checked to make sure that it is being set. Last I update $currentSum and assign
$totalToLoop = 10;
for ($x = 0; $x <= $totalToLoop; $x++) {
$currentSum = 1;
echo '1 ';
for ($y = 1; $y <= ($totalToLoop - $x); $y++) {
if (!isset($previous_line[$y])) {
$previous_line[$y] = 0;
}
printf('%d ', $currentSum = ($currentSum + $previous_line[$y]));
$previous_line[$y] = $currentSum;
}
echo '<br>';
}
results.
1 1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9 10
1 3 6 10 15 21 28 36 45
1 4 10 20 35 56 84 120
1 5 15 35 70 126 210
1 6 21 56 126 252
1 7 28 84 210
1 8 36 120
1 9 45
1 10
1

What makes this work?

I am trying to use this chunk of code as template, but Im not fully understanding of how one line works. I'll first provide the full chunk, then I'll single out the line I don't understand.
/** settings **/
$images_dir = 'preload-images/';
$thumbs_dir = 'preload-images-thumbs/';
$thumbs_width = 200;
$images_per_row = 3;
/** generate photo gallery **/
$image_files = get_files($images_dir);
if(count($image_files)) {
$index = 0;
foreach($image_files as $index=>$file) {
$index++;
$thumbnail_image = $thumbs_dir.$file;
if(!file_exists($thumbnail_image)) {
$extension = get_file_extension($thumbnail_image);
if($extension) {
make_thumb($images_dir.$file,$thumbnail_image,$thumbs_width);
}
}
echo '<img src="',$thumbnail_image,'" />';
if($index % $images_per_row == 0) { echo '<div class="clear"></div>'; }
}
echo '<div class="clear"></div>';
}
else {
echo '<p>There are no images in this gallery.</p>';
}
I understand how everything with the exception of this line works.
if($index % $images_per_row == 0) { echo '<div class="clear"></div>'; }
I know it is getting its value from this line:
$images_per_row = 3;
But what actually makes this work? Im still pretty new to php, and I would like a better understanding of the code Im about to use before I use it.
Any answers at all would be appreciative!
$index % $images_per_row == 0
The % means "mod", example 4 mod 2 = 0.
A % B = the remainder when we divide A by B.
In your script, the condition is met (valued to 'true') when the remainder of $index divided by $images_per_row equals 0, meaning the divisibility of $index by $images_per_row.
Hope it helps!
% is the modulo operator. It divides the two numbers and then returns the remainder after the division.
It is quite easy to understand if you remember your fractions and how to reduce them to their lowest terms.
So, if we make 5 % 2 into a fraction and reduce it:
5 1 (this is the remainder)
--- → 2 ---
2 2
So, 5 % 2 = 1.
If we take 8 % 3 we can do the same thing:
8 2 (this is the remainder)
--- → 2 ---
3 3
So, 8 % 3 = 2.
If there is no remainder, such as in 9 % 3 then you will get 0 back. See:
9 0 (this is the remainder)
--- → 3 ---
3 3
You can write some PHP to see what the values are when doing the modulo operations:
$perRow = 3;
for ($i = 0; $i < 10; $i++) {
echo "$i % $perRow = ", $i % $perRow, ' | ', "$i / $perRow = ", ($i / $perRow), "\n";
}
Output:
0 % 3 = 0 | 0 / 3 = 0
1 % 3 = 1 | 1 / 3 = 0.33333333333333
2 % 3 = 2 | 2 / 3 = 0.66666666666667
3 % 3 = 0 | 3 / 3 = 1
4 % 3 = 1 | 4 / 3 = 1.3333333333333
5 % 3 = 2 | 5 / 3 = 1.6666666666667
6 % 3 = 0 | 6 / 3 = 2
7 % 3 = 1 | 7 / 3 = 2.3333333333333
8 % 3 = 2 | 8 / 3 = 2.6666666666667
9 % 3 = 0 | 9 / 3 = 3

How to produce the following sequence of numbers through a php for statement

I need to find out a way to output the following number sequence through a double php for statement:
0 - 1 <-- begin
0 - 2
0 - 3
0 - 4
1 - 1
1 - 2
1 - 3
1 - 4
2 - 2
2 - 3
2 - 4
3 - 3
3 - 4 <-- end
How can I make the second loop, lose its first number, after each loop so the sequence looks like the output above?
I've been trying things like:
for($z = 0; $z <= 3; $z++) {
for($y = 0; $y <= 3; $y++) {
echo $z . " - " . $y . "<br />";
}
}
But the second loop keeps starting with a number 1. But I can't even think of a way to do it at all.
for ($x = 0; $x <= 3; $x++) {
for ($y = 1; $y <= 4; $y++) {
if ($y >= $x) {
echo "$x - $y\n";
}
}
}
Use a nested loop and let the nested one start at the iteration of the outer one:
for ($i = 0; $i < $max; $i++)
for ($j = $i; $j < $max; $j++)
echo "$i - $j";

checking if a number is divisible by 6 PHP

I want to check if a number is divisible by 6 and if not I need to increase it until it becomes divisible.
how can I do that ?
if ($number % 6 != 0) {
$number += 6 - ($number % 6);
}
The modulus operator gives the remainder of the division, so $number % 6 is the amount left over when dividing by 6. This will be faster than doing a loop and continually rechecking.
If decreasing is acceptable then this is even faster:
$number -= $number % 6;
if ($variable % 6 == 0) {
echo 'This number is divisible by 6.';
}:
Make divisible by 6:
$variable += (6 - ($variable % 6)) % 6; // faster than while for large divisors
$num += (6-$num%6)%6;
no need for a while loop! Modulo (%) returns the remainder of a division. IE 20%6 = 2. 6-2 = 4. 20+4 = 24. 24 is divisible by 6.
So you want the next multiple of 6, is that it?
You can divide your number by 6, then ceil it, and multiply it again:
$answer = ceil($foo / 6) * 6;
I see some of the other answers calling the modulo twice.
My preference is not to ask php to do the same thing more than once. For this reason, I cache the remainder.
Other devs may prefer to not generate the extra global variable or have other justifications for using modulo operator twice.
Code: (Demo)
$factor = 6;
for($x = 0; $x < 10; ++$x){ // battery of 10 tests
$number = rand( 0 , 100 );
echo "Number: $number Becomes: ";
if( $remainder = $number % $factor ) { // if not zero
$number += $factor - $remainder; // use cached $remainder instead of calculating again
}
echo "$number\n";
}
Possible Output:
Number: 80 Becomes: 84
Number: 57 Becomes: 60
Number: 94 Becomes: 96
Number: 48 Becomes: 48
Number: 80 Becomes: 84
Number: 36 Becomes: 36
Number: 17 Becomes: 18
Number: 41 Becomes: 42
Number: 3 Becomes: 6
Number: 64 Becomes: 66
Use the Mod % (modulus) operator
if ($x % 6 == 0) return 1;
function nearest_multiple_of_6($x) {
if ($x % 6 == 0) return $x;
return (($x / 6) + 1) * 6;
}
Simply run a while loop that will continue to loop (and increase the number) until the number is divisible by 6.
while ($number % 6 != 0) {
$number++;
}
Assuming $foo is an integer:
$answer = (int) (floor(($foo + 5) / 6) * 6)
For micro-optimisation freaks:
if ($num % 6 != 0)
$num += 6 - $num % 6;
More evaluations of %, but less branching/looping. :-P
Why don't you use the Modulus Operator?
Try this:
while ($s % 6 != 0) $s++;
Or is this what you meant?
<?
$s= <some_number>;
$k= $s % 6;
if($k !=0) $s=$s+6-$k;
?>
result = initial number + (6 - initial number % 6)

Categories