有多少种方法可以看出一个数字是否均匀,哪一个是最快最清晰的?

时间:2021-01-15 22:31:04

given any number, what's the best way to determine it is even? how many methods can you think of, and what is the fastest way and clearest way?

给出任何数字,确定它的最佳方法是什么?你能想到多少种方法,最快的方式和最清晰的方式是什么?

20 个解决方案

#1


bool isEven = ((number & 0x01) == 0)

The question said "any number", so one could either discard floats or handle them in another manner, perhaps by first scaling them up to an integral value first - watching out for overflow - i.e. change 2.1 to 21 (multiply by 10 and convert to int) and then test. It may be reasonable to assume, however, that by mentioning "any number" the person who posed the question is actually referring to integral values.

问题是“任何数字”,所以人们可以丢弃浮动或以另一种方式处理它们,也许首先将它们扩展到一个整数值 - 注意溢出 - 即改变2.1到21(乘以10并转换为int)然后测试。然而,可以合理地假设,通过提及“任何数字”,提出问题的人实际上是指整数值。

#2


bool isEven = number % 2 == 0;

#3


isEven(n) = ((-1) ^ n) == 1

where ^ is the exponentiation/pow function of your language.

其中^是您语言的指数/幂函数。

I didn't say it was fast or clear, but it has novelty value.

我没有说它快速或清晰,但它具有新颖的价值。

#4


The answer depends on the position being applied for. If you're applying for an Enterprise Architect position, then the following may be suitable:

答案取决于申请的职位。如果您正在申请企业架构师职位,那么以下内容可能适用:

First, you should create a proper Service-Oriented Architecture, as certainly the even-odd service won't be the only reusable component in your enterprise. An SOA consists of a service, interface, and service consumers. The service is function which can be invoked over the network. It exposes an interface contract and is typically registered with a Directory Service.

首先,您应该创建一个适当的面向服务的体系结构,因为奇怪的服务肯定不会是您企业中唯一可重用的组件。 SOA由服务,接口和服务使用者组成。该服务是可以通过网络调用的功能。它公开了接口合同,通常在目录服务中注册。

You can then create a Simple Object Access Protocol (SOAP) HTTP Web Service to expose your service.

然后,您可以创建一个简单对象访问协议(SOAP)HTTP Web服务来公开您的服务。

Next, you should prevent clients from directly calling your Web Service. If you allow this, then you will end up with a mess of point-to-point communication, which is very hard to maintain. Clients should access the Web Service through an Enterprise Service Bus (ESB).

接下来,您应该阻止客户端直接调用您的Web服务。如果你允许这样做,那么你最终会得到一堆点对点的通信,这很难维护。客户端应通过企业服务总线(ESB)访问Web服务。

In addition to providing a standard plug-able architecture, additional components like service orchestration can occur on the bus.

除了提供标准的可插拔架构外,还可以在总线上进行其他组件,如服务编排。

Generally, writing a bespoke even/odd service should be avoided. You should write a Request for proposal (RFP), and get several vendors to show you their even/odd service. The vendor's product should be able to plug into your ESB, and also provide you with an Service level agreement (SLA).

通常,应避免编写定制的偶数/奇数服务。您应该撰写提案请求(RFP),并让几个供应商向您展示他们的偶数/奇数服务。供应商的产品应该能够插入您的ESB,并为您提供服务级别协议(SLA)。

#5


This is even easier in ruby:

这在红宝石中更容易:

isEven = number.even?

#6


Yes.. The fastest way is to check the 1 bit, because it is set for all odd numbers and unset for all even numbers..

是..最快的方法是检查1位,因为它是为所有奇数设置的,并且对所有偶数都没有设置。

Bitwise ANDs are pretty fast.

按位AND非常快。

#7


If your type 'a' is an integral type, then we can define,

如果你的类型'a'是一个整数类型,那么我们可以定义,

even :: Integral a => a -> Bool
even n =  n `rem` 2 == 0

according to the Haskell Prelude.

根据Haskell Prelude。

#8


For floating points, of course within a reasonable bound.

对于浮点数,当然在合理范围内。

modf(n/2.0, &intpart, &fracpart)
return fracpart == 0.0

With some other random math functions:

使用其他一些随机数学函数:

return gcd(n,2) == 2

return lcm(n,2) == n

return cos(n*pi) == 1.0

#9


If int is 32 bits then you could do this:

如果int是32位,那么你可以这样做:

bool is_even = ((number << 31) >> 31) == 0;

With using bit shifts you'll shift the right-most bit to the left-most position and then back again, thus making all other bits 0's. Then the number you're left with is either 0 or 1. This method is somewhat similar to "number & 1" method where you again make all bits 0's except the first one.

通过使用位移,您可以将最右侧的位移到最左侧的位置,然后再移回,从而使所有其他位为0。然后你剩下的数字是0或1.这个方法有点类似于“数字和1”方法,你再次使所有位0除了第一个。

Another approach, similar to this one is this:

另一种类似于此的方法是这样的:

bool is_even = (number << 31) == 0;

or

bool is_odd = (number << 31) < 0;

If the number is even (the right-most bit is 0), then shifting it 31 positions will make the whole number 0. If the bit is 1, i.e. the number is odd, then the resulting number would be negative (every integer with left-most bit 1 is negative except if the number is of type unsigned, where it won't work). To fix signed/unsigned bug, you can just test:

如果数字是偶数(最右边的位是0),那么将它移位31个位置将使整数为0.如果该位为1,即数字为奇数,那么得到的数字将为负数(每个整数为最左边的位1是负数,除非该数字是无符号类型,否则它将不起作用)。要修复签名/未签名的错误,您只需测试:

bool is_odd = (number << 31) != 0;

#10


Actually I think (n % 2 == 0) is enough, which is easy to understand and most compilers will convert it to bit operations as well.

实际上我认为(n%2 == 0)就足够了,这很容易理解,大多数编译器也将它转换为位操作。

I compiled this program with gcc -O2 flag:

我用gcc -O2标志编译了这个程序:

#include <stdio.h>

int main()
{
    volatile int x = 310;
    printf("%d\n", x % 2);
    return 0;
}

and the generated assembly code is

并生成的汇编代码是

main:
    pushl   %ebp
    movl    %esp, %ebp
    andl    $-16, %esp
    subl    $32, %esp
    movl    $310, 28(%esp)
    movl    28(%esp), %eax
    movl    $.LC0, (%esp)
    movl    %eax, %edx
    shrl    $31, %edx
    addl    %edx, %eax
    andl    $1, %eax
    subl    %edx, %eax
    movl    %eax, 4(%esp)
    call    printf
    xorl    %eax, %eax
    leave
    ret

which we can see that % 2 operation is already converted to the andl instruction.

我们可以看到%2操作已经转换为andl指令。

#11


Similar to DeadHead's comment, but more efficient:

与DeadHead的评论相似,但效率更高:

#include <limits.h>

bool isEven(int num)
{
    bool arr[UINT_MAX] = { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           // ...and so on
    };
    return arr[num];
}

As fast as an array index, which may or may not be faster than bitwise computations (it's difficult to test because I don't want to write the full version of this function). For what it's worth, that function above only has enough filled in to find even numbers up to 442, but would have to go to 4294967295 to work on my system.

和数组索引一样快,它可能比也可能不比按位计算快(因为我不想编写这个函数的完整版本,所以很难测试)。对于它的价值,上面的功能只有足够的内容才能找到最多442的偶数,但是必须转到4294967295来处理我的系统。

#12


With reservations for limited stack space. ;) (Is this perhaps a candidate for tail calls?)

预订有限的堆栈空间。 ;)(这可能是尾调用的候选者吗?)

public static bool IsEven(int num) {
    if (num < 0)
        return !IsEven(-num - 1);

    if (num == 0)
        return true;

    return IsEven(-num);
}

#13


a % 2.

a2。

  1. It's clear
  2. It's fast on every decent compiler.
  3. 它在每个体面的编译器上都很快。

Everyone who cries "But! But! What if compiler doesn't optimize it" should find normal compiler, shut up and read about premature optimization, read again, read again.

每个人都在喊“但是!但是!如果编译器不优化它会怎么样”应该找到正常的编译器,关闭并读取有关过早优化,再次读取,再次读取。

#14


If it's low level check if the last (LSB) bit is 0 or 1 :)

如果它是低电平检查最后一个(LSB)位是0还是1 :)

0 = Even
1 = Odd

0 =偶数1 =奇数

Otherwise, +1 @sipwiz: "bool isEven = number % 2 == 0;"

否则,+1 @sipwiz:“bool isEven = number%2 == 0;”

#15


Assumming that you are dealing with an integer, the following will work:

假设您正在处理整数,以下内容将起作用:

if ((testnumber & -2)==testnumber) then testnumber is even.

如果((testnumber&-2)== testnumber)那么testnumber是偶数。

basically, -2 in hex will be FFFE (for 16 bits) if the number is even, then anding with with -2 will leave it unchanged. ** Tom **

基本上,如果数字为偶数,则十六进制中的-2将为FFFE(对于16位),然后使用-2进行保持将保持不变。 **汤姆**

#16


You can either using integer division and divide it by two and inspect the remainder or use a modulus operator and mod it by two and inspect the remainder. The "fastest" way depends on the language, compiler, and other factors but I doubt there are many platforms for which there is a significant difference.

您可以使用整数除法并将其除以2并检查余数或使用模数运算符并将其修改为2并检查余数。 “最快”的方式取决于语言,编译器和其他因素,但我怀疑有许多平台存在显着差异。

#17


Recursion!

function is_even (n number) returns boolean is
  if n = 0 then
     return true
  elsif n = 1 then
     return false
  elsif n < 0 then
     return is_even(n * -1)
  else
     return is_even(n - 2)
  end if
end

#18


Continuing the spirit of "how many ways are there...":

继续“有多少种方式......”的精神:

function is_even (n positive_integer) returns boolean is
  i := 0
  j := 0
  loop
    if n = i then
       return (j = 0)
    end if;
    i := i + 1
    j := 1 - j
  end loop
end

#19


In response to Chris Lutz, an array lookup is significantly slower than a BITWISE_AND operation. In an array lookup you're doing a memory lookup which will always be slower than a bitwise operation because of memory latency. This of course doesn't even factor in the problem of putting all possible int values into your array which has a memory complexity of O(2^n) where n is your bus size (8,16,32,64).

在回应Chris Lutz时,数组查找明显慢于BITWISE_AND操作。在数组查找中,您正在进行内存查找,由于内存延迟,它总是比按位操作慢。这当然不会影响将所有可能的int值放入数组的问题,该数组的内存复杂度为O(2 ^ n),其中n是总线大小(8,16,32,64)。

The odd/even property is only defined in integers. So any answer dealing with floating point is invalid. The abstract representation of this problem is Int -> bool (to use Haskell notation).

奇数/偶数属性仅以整数定义。所以任何处理浮点的答案都是无效的。这个问题的抽象表示是Int - > bool(使用Haskell表示法)。

#20


Another useless novelty solution:

另一个无用的新奇解决方案:

if (2 * (n/2) == n)
    return true;
else
    return false;

Only with integers, and it depends on how the langugage handles integer division.

只有整数,这取决于langugage如何处理整数除法。

n/2 == n/2 if it's even or n/2-.5 if it's odd. So 2*(n/2) == n if it's even or n - 1 if it's odd.

n / 2 == n / 2如果是偶数或n / 2-.5如果它是奇数。所以2 *(n / 2)== n,如果它是偶数,或者n - 1,如果它是奇数。

#1


bool isEven = ((number & 0x01) == 0)

The question said "any number", so one could either discard floats or handle them in another manner, perhaps by first scaling them up to an integral value first - watching out for overflow - i.e. change 2.1 to 21 (multiply by 10 and convert to int) and then test. It may be reasonable to assume, however, that by mentioning "any number" the person who posed the question is actually referring to integral values.

问题是“任何数字”,所以人们可以丢弃浮动或以另一种方式处理它们,也许首先将它们扩展到一个整数值 - 注意溢出 - 即改变2.1到21(乘以10并转换为int)然后测试。然而,可以合理地假设,通过提及“任何数字”,提出问题的人实际上是指整数值。

#2


bool isEven = number % 2 == 0;

#3


isEven(n) = ((-1) ^ n) == 1

where ^ is the exponentiation/pow function of your language.

其中^是您语言的指数/幂函数。

I didn't say it was fast or clear, but it has novelty value.

我没有说它快速或清晰,但它具有新颖的价值。

#4


The answer depends on the position being applied for. If you're applying for an Enterprise Architect position, then the following may be suitable:

答案取决于申请的职位。如果您正在申请企业架构师职位,那么以下内容可能适用:

First, you should create a proper Service-Oriented Architecture, as certainly the even-odd service won't be the only reusable component in your enterprise. An SOA consists of a service, interface, and service consumers. The service is function which can be invoked over the network. It exposes an interface contract and is typically registered with a Directory Service.

首先,您应该创建一个适当的面向服务的体系结构,因为奇怪的服务肯定不会是您企业中唯一可重用的组件。 SOA由服务,接口和服务使用者组成。该服务是可以通过网络调用的功能。它公开了接口合同,通常在目录服务中注册。

You can then create a Simple Object Access Protocol (SOAP) HTTP Web Service to expose your service.

然后,您可以创建一个简单对象访问协议(SOAP)HTTP Web服务来公开您的服务。

Next, you should prevent clients from directly calling your Web Service. If you allow this, then you will end up with a mess of point-to-point communication, which is very hard to maintain. Clients should access the Web Service through an Enterprise Service Bus (ESB).

接下来,您应该阻止客户端直接调用您的Web服务。如果你允许这样做,那么你最终会得到一堆点对点的通信,这很难维护。客户端应通过企业服务总线(ESB)访问Web服务。

In addition to providing a standard plug-able architecture, additional components like service orchestration can occur on the bus.

除了提供标准的可插拔架构外,还可以在总线上进行其他组件,如服务编排。

Generally, writing a bespoke even/odd service should be avoided. You should write a Request for proposal (RFP), and get several vendors to show you their even/odd service. The vendor's product should be able to plug into your ESB, and also provide you with an Service level agreement (SLA).

通常,应避免编写定制的偶数/奇数服务。您应该撰写提案请求(RFP),并让几个供应商向您展示他们的偶数/奇数服务。供应商的产品应该能够插入您的ESB,并为您提供服务级别协议(SLA)。

#5


This is even easier in ruby:

这在红宝石中更容易:

isEven = number.even?

#6


Yes.. The fastest way is to check the 1 bit, because it is set for all odd numbers and unset for all even numbers..

是..最快的方法是检查1位,因为它是为所有奇数设置的,并且对所有偶数都没有设置。

Bitwise ANDs are pretty fast.

按位AND非常快。

#7


If your type 'a' is an integral type, then we can define,

如果你的类型'a'是一个整数类型,那么我们可以定义,

even :: Integral a => a -> Bool
even n =  n `rem` 2 == 0

according to the Haskell Prelude.

根据Haskell Prelude。

#8


For floating points, of course within a reasonable bound.

对于浮点数,当然在合理范围内。

modf(n/2.0, &intpart, &fracpart)
return fracpart == 0.0

With some other random math functions:

使用其他一些随机数学函数:

return gcd(n,2) == 2

return lcm(n,2) == n

return cos(n*pi) == 1.0

#9


If int is 32 bits then you could do this:

如果int是32位,那么你可以这样做:

bool is_even = ((number << 31) >> 31) == 0;

With using bit shifts you'll shift the right-most bit to the left-most position and then back again, thus making all other bits 0's. Then the number you're left with is either 0 or 1. This method is somewhat similar to "number & 1" method where you again make all bits 0's except the first one.

通过使用位移,您可以将最右侧的位移到最左侧的位置,然后再移回,从而使所有其他位为0。然后你剩下的数字是0或1.这个方法有点类似于“数字和1”方法,你再次使所有位0除了第一个。

Another approach, similar to this one is this:

另一种类似于此的方法是这样的:

bool is_even = (number << 31) == 0;

or

bool is_odd = (number << 31) < 0;

If the number is even (the right-most bit is 0), then shifting it 31 positions will make the whole number 0. If the bit is 1, i.e. the number is odd, then the resulting number would be negative (every integer with left-most bit 1 is negative except if the number is of type unsigned, where it won't work). To fix signed/unsigned bug, you can just test:

如果数字是偶数(最右边的位是0),那么将它移位31个位置将使整数为0.如果该位为1,即数字为奇数,那么得到的数字将为负数(每个整数为最左边的位1是负数,除非该数字是无符号类型,否则它将不起作用)。要修复签名/未签名的错误,您只需测试:

bool is_odd = (number << 31) != 0;

#10


Actually I think (n % 2 == 0) is enough, which is easy to understand and most compilers will convert it to bit operations as well.

实际上我认为(n%2 == 0)就足够了,这很容易理解,大多数编译器也将它转换为位操作。

I compiled this program with gcc -O2 flag:

我用gcc -O2标志编译了这个程序:

#include <stdio.h>

int main()
{
    volatile int x = 310;
    printf("%d\n", x % 2);
    return 0;
}

and the generated assembly code is

并生成的汇编代码是

main:
    pushl   %ebp
    movl    %esp, %ebp
    andl    $-16, %esp
    subl    $32, %esp
    movl    $310, 28(%esp)
    movl    28(%esp), %eax
    movl    $.LC0, (%esp)
    movl    %eax, %edx
    shrl    $31, %edx
    addl    %edx, %eax
    andl    $1, %eax
    subl    %edx, %eax
    movl    %eax, 4(%esp)
    call    printf
    xorl    %eax, %eax
    leave
    ret

which we can see that % 2 operation is already converted to the andl instruction.

我们可以看到%2操作已经转换为andl指令。

#11


Similar to DeadHead's comment, but more efficient:

与DeadHead的评论相似,但效率更高:

#include <limits.h>

bool isEven(int num)
{
    bool arr[UINT_MAX] = { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1,
                           0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0,
                           // ...and so on
    };
    return arr[num];
}

As fast as an array index, which may or may not be faster than bitwise computations (it's difficult to test because I don't want to write the full version of this function). For what it's worth, that function above only has enough filled in to find even numbers up to 442, but would have to go to 4294967295 to work on my system.

和数组索引一样快,它可能比也可能不比按位计算快(因为我不想编写这个函数的完整版本,所以很难测试)。对于它的价值,上面的功能只有足够的内容才能找到最多442的偶数,但是必须转到4294967295来处理我的系统。

#12


With reservations for limited stack space. ;) (Is this perhaps a candidate for tail calls?)

预订有限的堆栈空间。 ;)(这可能是尾调用的候选者吗?)

public static bool IsEven(int num) {
    if (num < 0)
        return !IsEven(-num - 1);

    if (num == 0)
        return true;

    return IsEven(-num);
}

#13


a % 2.

a2。

  1. It's clear
  2. It's fast on every decent compiler.
  3. 它在每个体面的编译器上都很快。

Everyone who cries "But! But! What if compiler doesn't optimize it" should find normal compiler, shut up and read about premature optimization, read again, read again.

每个人都在喊“但是!但是!如果编译器不优化它会怎么样”应该找到正常的编译器,关闭并读取有关过早优化,再次读取,再次读取。

#14


If it's low level check if the last (LSB) bit is 0 or 1 :)

如果它是低电平检查最后一个(LSB)位是0还是1 :)

0 = Even
1 = Odd

0 =偶数1 =奇数

Otherwise, +1 @sipwiz: "bool isEven = number % 2 == 0;"

否则,+1 @sipwiz:“bool isEven = number%2 == 0;”

#15


Assumming that you are dealing with an integer, the following will work:

假设您正在处理整数,以下内容将起作用:

if ((testnumber & -2)==testnumber) then testnumber is even.

如果((testnumber&-2)== testnumber)那么testnumber是偶数。

basically, -2 in hex will be FFFE (for 16 bits) if the number is even, then anding with with -2 will leave it unchanged. ** Tom **

基本上,如果数字为偶数,则十六进制中的-2将为FFFE(对于16位),然后使用-2进行保持将保持不变。 **汤姆**

#16


You can either using integer division and divide it by two and inspect the remainder or use a modulus operator and mod it by two and inspect the remainder. The "fastest" way depends on the language, compiler, and other factors but I doubt there are many platforms for which there is a significant difference.

您可以使用整数除法并将其除以2并检查余数或使用模数运算符并将其修改为2并检查余数。 “最快”的方式取决于语言,编译器和其他因素,但我怀疑有许多平台存在显着差异。

#17


Recursion!

function is_even (n number) returns boolean is
  if n = 0 then
     return true
  elsif n = 1 then
     return false
  elsif n < 0 then
     return is_even(n * -1)
  else
     return is_even(n - 2)
  end if
end

#18


Continuing the spirit of "how many ways are there...":

继续“有多少种方式......”的精神:

function is_even (n positive_integer) returns boolean is
  i := 0
  j := 0
  loop
    if n = i then
       return (j = 0)
    end if;
    i := i + 1
    j := 1 - j
  end loop
end

#19


In response to Chris Lutz, an array lookup is significantly slower than a BITWISE_AND operation. In an array lookup you're doing a memory lookup which will always be slower than a bitwise operation because of memory latency. This of course doesn't even factor in the problem of putting all possible int values into your array which has a memory complexity of O(2^n) where n is your bus size (8,16,32,64).

在回应Chris Lutz时,数组查找明显慢于BITWISE_AND操作。在数组查找中,您正在进行内存查找,由于内存延迟,它总是比按位操作慢。这当然不会影响将所有可能的int值放入数组的问题,该数组的内存复杂度为O(2 ^ n),其中n是总线大小(8,16,32,64)。

The odd/even property is only defined in integers. So any answer dealing with floating point is invalid. The abstract representation of this problem is Int -> bool (to use Haskell notation).

奇数/偶数属性仅以整数定义。所以任何处理浮点的答案都是无效的。这个问题的抽象表示是Int - > bool(使用Haskell表示法)。

#20


Another useless novelty solution:

另一个无用的新奇解决方案:

if (2 * (n/2) == n)
    return true;
else
    return false;

Only with integers, and it depends on how the langugage handles integer division.

只有整数,这取决于langugage如何处理整数除法。

n/2 == n/2 if it's even or n/2-.5 if it's odd. So 2*(n/2) == n if it's even or n - 1 if it's odd.

n / 2 == n / 2如果是偶数或n / 2-.5如果它是奇数。所以2 *(n / 2)== n,如果它是偶数,或者n - 1,如果它是奇数。