Checking for increments in PHP [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Using PHP, if I have a starting value, how can I check to see if another value is within a certain number of increments away?
In other words if I start with the value 100 and have an increment of 5, then 90, 95, 105, 110 would all be acceptable, but 104 would not.

if($number %5 == 0) {
//CODE
}
This gets the remainder of $number/5. If it equals 0 then obviously it is divisible by 5 and thus is in your sequence.

You want to take the mod of the inputted number and compare it to the mod of the other number and see if they are the same. For example if you start with 99,
99 % 5 == 4
so now you can see all the increments by checking the mod of them in comparison.
104 % 5 == 4
109 % 5 == 4
So store the mod of the input number vs the mod number and then check to see if it is the same as the mod of the new number. I would write you a php example, but I don't use php.

Related

MySQL mod vs PHP mod [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Edited
Sorry guys I want PHP mod to display decimals
In MySQL select mod(15.6,1) result 0.6 but in PHP 15.6 % 1 result 0?
My purpose is to restrict calculation only for exact integer which does not allow decimals
In PHP, the % modulus operator works on integers. The operands 15.6 and 1 are cast to int (15 and 1) before being worked on. Therefore, 15 % 1, as you may expect, gives 0. Therefore "exact integer which does not allow decimals" matches the behaviour in PHP.
In MySQL, as the documentation says:
MOD() also works on values that have a fractional part and returns the exact remainder after division
Therefore, you should TRUNCATE(operand, 0) the operands before using them in MySQL to obtain the integer behaviour. This is consistent with PHP's behaviour of casting to int -- numbers are truncated, not floored (which is a significant difference with negative numbers).

How to generate this series? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
What type of series it is and how to generate this by php program?
0 1 3 2 6 7 5 4 12 13 15 14 ...
Observation: The successive difference of the entity is 1
Example:
Difference of 0 and 1 is 1
Difference of 3 and 2 is 1
Difference of 6 and 7 is 1
Difference of 5 and 4 is 1
Difference of 12 and 13 is 1
Difference of 15 and 14 is 1
Please help ...
Its a Decimal Equivalent of Gray code up to n. I have written a code to generate the Gray code for any Number, Use this to generate a series. I have used Javascript, but you can choose any language you want.
Number.toGrayCode = function(n) {
if (n < 0) {
throw new RangeError("cannot convert negative numbers to gray code");
}
return n ^ (n >>> 1);
};
for( var i=0;i<=10;i++)
console.log(Number.toGrayCode(i));

How to get out number - php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to find out how I can extract $number out of the following code:
$calc = ($number % 10000) / 100;
So what's the opposite of %?
%, or the modulo operator, means "remainder after division by".
For example: 18 % 4 = 2, because if you divide 18 by 4 you get 4 with a remainder of 2. In the other direction, we can see that indeed 4 * 4 + 2 = 18.
Now consider the following:
18 % 4 = 2
22 % 4 = 2
26 % 4 = 2
...
2445678 % 4 = 2
As you can see, there are multiple values that are what we call congruent modulo 4. Therefore an inverse function of modulo cannot exist, because there is an infinite amount of possibilities.
Several cryptographic functions are based on the fact that the above holds true.
Generally you can't. % gives you the modulo of a division, there are mathematically an infinity of numbers that have the same modulo for a given ratio.
It is simply not bijective..

How to check if number of rows = 6n-1 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
I wish to count a number of rows in my sql database, and if the number of rows equal 6n-1 perform a function, if not, do nothing.
I know I can specify the integers to look for, but there would be thousands, so I wish to find a way where I can just define the math 6n-1
for example, when I count the rows in a table, I want to check whether there is 5,11,17 and so on.
If the number of rows match, do something, else do nothing.
Since 5,11,17.... is equal to every 6th row starting from 5, this can be expressed as 6n-1.
IS there a way to do this without defining every integer to check?
($rowcount + 1) % 6 == 0
perhaps...
for ( $i=1 ; 6*$i-1 <= $database_rows ; $i++ ) {
if ( 6*$i-1 == $database_rows ) {
do_something();
}
}

'IF' statement doesn't work right? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
When I print out the integers (1 or 0), all the iterations (there are 3 of them in this case) print out the correct number. First and the second one return 1, the last one returns zero. BUT, 'if' statement seems to display everything in it anyway. What am I doing wrong?
All the code below is inside a bigger 'for' loop.
$yn = 0;
if(!in_array($pos, $blocks)){
$blocks[$x] = $pos;
$x++;
$yn = 1;
}
print_r($blocks);
print "YN: ".$yn; # this prints out 1, 1 and 0 on the last iteration
if(yn){
# show some stuff (is displayed in all three iterations, but it shouldn't be on the last)
}
Try:
if($yn){
PHP is interpreting yn as a string, rather than as the variable you should be using.

Categories