Simplest way to calculate amount of even numbers in given range

前端 未结 16 2430
小蘑菇
小蘑菇 2021-02-13 14:10

What is the simplest way to calculate the amount of even numbers in a range of unsigned integers?

An example: if range is [0...4] then the answer is 3 (0,2,4)

I\

16条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-13 14:53

    This does not require any conditions at all:

    evencount = floor((max - min)/2) + 1
    

提交回复
热议问题