Simplest way to calculate amount of even numbers in given range

前端 未结 16 2396
小蘑菇
小蘑菇 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:39

    Hint 1: The modulo operator will return the remainder of the current number
    Hint 2: You don't need a for loop
    Hint 3: A range is continuous
    Hint 4: The number of even numbers in a continuous range are half even (sometimes half + 1, sometimes half - 1)
    Hint 5: Building on Hint1: Consider also what (being + end + 1) % 2 gives
    Hint 6: Most or all of the answers in this thread are wrong
    Hint 7: Make sure you try your solution with negative number ranges
    Hint 8: Make sure you try your solution with ranges spanning both negative and positive numbers

    0 讨论(0)
  • 2021-02-13 14:44
    int even = (0 == begin % 2) ? (end - begin) / 2 + 1 : (end - begin + 1) / 2;
    

    Which can be converted into:

    int even = (end - begin + (begin % 2)) / 2 + (1 - (begin % 2));
    

    EDIT: This can further simplified into:

    int even = (end - begin + 2 - (begin % 2)) / 2;
    

    EDIT2: Because of the in my opinion somewhat incorrect definition of integer division in C (integer division truncates downwards for positive numbers and upwards for negative numbers) this formula won't work when begin is a negative odd number.

    EDIT3: User 'iPhone beginner' correctly observes that if begin % 2 is replaced with begin & 1 this will work correctly for all ranges.

    0 讨论(0)
  • 2021-02-13 14:44

    Answer is to use binary AND.

    so a number is represented in memory in 0 and 1. lets say 4 and 5.

    4 = 0000 0100

    5 = 0000 0101

    and every even number has a zero in the end and every odd number has 1 in the end;

    in c '1' means true and '0' means false.

    so: lets code;

    function isEven(int num){
         return ((num & 0x01) == 0) ? 1 : 0;
    }
    

    Here 0x01 means 0000 0001. so we are anding 0x01 with the given number.

    imagine no is 5

    5    |0000 0101 
    
    0x01 |0000 0001
    
    ---------------
    
          0000 0001
    

    so answer will be '1'.

    imagine no is 4

    4    |0000 0100 
    
    0x01 |0000 0001
    
    ---------------
    
          0000 0000
    

    so answer will be '0'.

    now,

    return ((num & 0x01) == 0) ? 1 : 0;
    

    it is expanded in :

    if((num & 0x01) == 0){// means  the number is even
          return 1;
    }else{//means no is odd
          return 0;
    }
    

    So this is all.

    End is binary operatiors are very important in compititive programming world.

    happy coding.

    first answer here.

    EDIT 1:

    Total no of evens are

    totalEvens = ((end - start) / 2 + ((((end - start) & 0x01 ) == 0) ? 0 : 1 ));
    

    here (end - start)/2 gives the half of total numbers.

    this works if one is even and one is odd.

    but,

    ((((end - start) & 0x01 ) == 0) ? 0 : 1 )
    

    can just replaced by (!isEven(end-start))

    So, if the total number is even then dont add 1 else add 1.

    this completely works.

    0 讨论(0)
  • 2021-02-13 14:44

    The range is always [2a+b, 2c+d] with b,d = {0,1}. Make a table:

    b d | #even
    0 0 | c-a+1
    0 1 | c-a+1
    1 0 | c-a
    1 1 | c-a+1
    

    Now a = min/2, b = min % 2, c = max/2 and d = max % 2.

    So int nEven = max/2 - min/2 + 1 - (min%2).

    0 讨论(0)
  • 2021-02-13 14:44

    In terms of start and length:

    (length >> 1) + (1 & ~start & length)

    half the length plus 1 if start is even and length is odd.

    In terms of start and end:

    ((end - start + 1) >> 1) + (1 & ~start & ~end)

    half the length plus 1 if start is even and end is even.

    0 讨论(0)
  • 2021-02-13 14:46

    I'm a bit surprised that iteration was tried to solve this. The minimum number of even numbers possible in a range is equal to half of the length of the array of numbers, or, rangeEnd - rangeStart.
    Add 1 if the first or last number is even.

    So the method is: (using javascript)

    function evenInRange(rangeStart, rangeEnd)
    {
      return
        Math.floor(rangeEnd - rangeStart) + 
        ((rangeStart % 2 == 0) || (rangeEnd % 2 == 0) ? 1 : 0)
    }
    
    
    Tests:
    0 1 2 3 4 5 6 7 8
    8 - 0 = 8
    8 / 2 = 4
    4 + 1 = 5
    Even numbers in range:
    0 2 4 6 8
    
    11 12 13 14 15 16 17 18 19 20
    20 - 11 = 9
    9 / 2 = 4
    4 + 1 = 5
    Even numbers in range
    12 14 16 18 20
    
    1 2 3
    3 - 1 = 2
    2 / 2 = 1
    1 + 0 = 1
    Even numbers in range
    2
    
    2 3 4 5
    5 - 2 = 3
    3 / 2 = 1
    1 + 1 = 2
    Even numbers in range
    2 4
    
    2 3 4 5 6
    6 - 2 = 4
    4 / 2 = 2
    2 + 1 = 3
    Even numbers in range
    2 4 6
    
    0 讨论(0)
提交回复
热议问题