• bob_lemon
    link
    fedilink
    arrow-up
    52
    ·
    edit-2
    21 days ago
    import re
    
    def is_even(i: int) -> bool:
        return re.match(r"-?\d*[02468]$", str(i)) is not None
    
  • superkret
    link
    fedilink
    arrow-up
    45
    ·
    21 days ago

    Just divide the number into its prime factors and then check if one of them is 2.

    • fartripper@lemmy.ml
      link
      fedilink
      arrow-up
      20
      ·
      edit-2
      21 days ago

      or divide the number by two and if the remainder is greater than

      -(4^34)
      

      but less than

      70 - (((23*3*4)/2)/2)
      

      then

      true
      
      • superkret
        link
        fedilink
        arrow-up
        8
        ·
        21 days ago

        What if the remainder is greater than the first, but not less than the latter?

        Like, for example, 1?

        • prime_number_314159@lemmy.world
          link
          fedilink
          arrow-up
          3
          ·
          21 days ago

          Then you should return false, unless the remainder is also greater than or equal to the twenty second root of 4194304. Note, that I’ve only checked up to 4194304 to make sure this works, so if you need bigger numbers, you’ll have to validate on your own.

            • prime_number_314159@lemmy.world
              link
              fedilink
              arrow-up
              1
              ·
              21 days ago

              You can just bitwise AND those with …000000001 (for however many bits are in your number). If the result is 0, then the number is even, and if it’s 1, then the number is odd. This works for negative numbers because it discards the negative signing bit.

    • tipicaldik@lemmy.world
      link
      fedilink
      arrow-up
      15
      arrow-down
      1
      ·
      21 days ago

      I remember coding actionscript in Flash and using modulo (%) to determine if a number was even or odd. It returns the remainder of the number divided by 2 and if it equals anything other than 0 then the number is odd.

      • Korne127@lemmy.world
        link
        fedilink
        arrow-up
        25
        ·
        21 days ago

        Yeah. The joke is that this is the obvious solution always used in practise, but the programmer is that bad that they don’t know it and use some ridiculous alternative solutions instead.

  • lnxtx@feddit.nl
    link
    fedilink
    English
    arrow-up
    23
    arrow-down
    1
    ·
    21 days ago

    Ask AI:

    public static boolean isEven(int number) {
        // Handle negative numbers
        if (number < 0) {
            number = -number; // Convert to positive
        }
        
        // Subtract 2 until we reach 0 or 1
        while (number > 1) {
            number -= 2;
        }
        
        // If we reach 0, it's even; if we reach 1, it's odd
        return number == 0;
    }
    
  • gerryflap@feddit.nl
    link
    fedilink
    English
    arrow-up
    23
    arrow-down
    1
    ·
    20 days ago

    Using Haskell you can write it way more concise:

    iseven :: Int -> Bool
    iseven 0 = True
    iseven 1 = False
    iseven 2 = True
    iseven 3 = False
    iseven 4 = True
    iseven 5 = False
    iseven 6 = True
    iseven 7 = False
    iseven 8 = True
    ...
    

    However, we can be way smarter by only defining the 2 base cases and then a recursive definition for all other numbers:

    iseven :: Int -> Bool
    iseven 0 = True
    iseven 1 = False
    iseven n = iseven (n-2)
    

    It’s having a hard time with negative numbers, but honestly that’s quite a mood

  • affiliate@lemmy.world
    link
    fedilink
    arrow-up
    23
    arrow-down
    1
    ·
    21 days ago

    a wise programmer knows to always ask the question “can i solve this problem in python using metaprogramming?” in this instance, the answer is yes:

    def is_even(n: int):
        s = "def is_even_helper(number: int):\n"
        b = True
        for i in range(0, abs(n)+2):
            s += f"\tif (abs(number) == {i}): return {b}\n"
            b = not b
        exec(s)
        return locals().get("is_even_helper")(n)
    
  • istdaslol
    link
    fedilink
    arrow-up
    18
    ·
    21 days ago

    When you sacrifice memory for an O(1) algorithm.

    In this case still O(n)

    • istdaslol
      link
      fedilink
      arrow-up
      4
      ·
      21 days ago

      Not all ARM CPUs support mod operations. It’s better to use bit operations. Check if the last bit is set. If set it’s odd else it’s even.

    • Fiona@discuss.tchncs.de
      link
      fedilink
      arrow-up
      13
      ·
      edit-2
      20 days ago

      And that isn’t even the worst thing about it…

      The implementation looks like this:

      function isEven(i) {
        return !isOdd(i);
      };
      

      And yes, is-odd is a dependency that in turn depends on is-number

    • Micromot
      link
      fedilink
      English
      arrow-up
      8
      arrow-down
      1
      ·
      edit-2
      21 days ago

      Can’t you just

      If (number % 2 == 0){return true}

      • drake@lemmy.sdf.org
        link
        fedilink
        arrow-up
        6
        ·
        20 days ago

        but what if number isn’t an integer, or even a number at all? This code, and the improved code shared by the other user, could cause major problems under those conditions. Really, what you would want, is to validate that number is actually an integer before performing the modulo, and if it isn’t, you want to throw an exception, because something has gone wrong.

        That’s exactly what that NPM module does. And this is why it’s not a bad thing to use packages/modules for even very simple tasks, because they help to prevent us from making silly mistakes.