Not a request for code of any type.
不是任何类型的代码请求。
Let's say you are randomly generating a number between zero and ten and are doing so until you receive just an even number. Is zero an available result?
假设您正在随机生成0到10之间的数字,并且这样做直到您只收到偶数。零是可用的结果吗?
Are the evens 0-10:
是均匀0-10:
2, 4, 6, 8, 10
-OR-
0, 2, 4, 6, 8, 10
3 个解决方案
#1
Mathematically, 0 is even. PHP has no built-in notion of even.
在数学上,0是偶数。 PHP没有内置的概念。
A (mathematically correct) test for even parity is checking if the number vanishes mod 2. This test is implemented as $number % 2 == 0
. For zero you can quickly check that 0 mod 2 is 0, and would be considered even.
偶数奇偶校验的(数学上正确的)测试是检查数字是否消失mod 2.此测试实现为$ number%2 == 0.对于零,您可以快速检查0 mod 2是否为0,并且将被视为偶数。
#2
Zero is an even number. So, yes, it's even "in PHP," too. As a test, 0 % 2 === 0
=> 0
is even.
零是偶数。所以,是的,它甚至“在PHP中”。作为测试,0%2 === 0 => 0是偶数。
#3
This might be a month late but the best way to determine if a number is even or odd is by using a simple mathematical formula.
这可能是一个月晚了,但确定数字是偶数还是奇数的最佳方法是使用简单的数学公式。
Even: 2n or 2 x n;
Odd: 2n+1 or (2 x n)+1;
Using the two equations above (where n>=0), you can determine that 0 is even.
使用上面的两个等式(其中n> = 0),您可以确定0是偶数。
Even: 2(0) = 0;
Odd: 2(0)+1 = 1;
So using the lowest possible number where n is greater than or equal to zero, it is proven that 0 is even.
因此,使用n大于或等于零的最低可能数,证明0是偶数。
#1
Mathematically, 0 is even. PHP has no built-in notion of even.
在数学上,0是偶数。 PHP没有内置的概念。
A (mathematically correct) test for even parity is checking if the number vanishes mod 2. This test is implemented as $number % 2 == 0
. For zero you can quickly check that 0 mod 2 is 0, and would be considered even.
偶数奇偶校验的(数学上正确的)测试是检查数字是否消失mod 2.此测试实现为$ number%2 == 0.对于零,您可以快速检查0 mod 2是否为0,并且将被视为偶数。
#2
Zero is an even number. So, yes, it's even "in PHP," too. As a test, 0 % 2 === 0
=> 0
is even.
零是偶数。所以,是的,它甚至“在PHP中”。作为测试,0%2 === 0 => 0是偶数。
#3
This might be a month late but the best way to determine if a number is even or odd is by using a simple mathematical formula.
这可能是一个月晚了,但确定数字是偶数还是奇数的最佳方法是使用简单的数学公式。
Even: 2n or 2 x n;
Odd: 2n+1 or (2 x n)+1;
Using the two equations above (where n>=0), you can determine that 0 is even.
使用上面的两个等式(其中n> = 0),您可以确定0是偶数。
Even: 2(0) = 0;
Odd: 2(0)+1 = 1;
So using the lowest possible number where n is greater than or equal to zero, it is proven that 0 is even.
因此,使用n大于或等于零的最低可能数,证明0是偶数。