• Skyline969@lemmy.ca
    link
    fedilink
    English
    arrow-up
    53
    ·
    edit-2
    1 year ago

    Wow. Amateur hour over here. There’s a much easier way to write this.

    A case select:

    select(number){
        case 1:
            return false;
        case 2:
            return true;
    }
    

    And so on.

  • lobut@lemmy.ca
    link
    fedilink
    arrow-up
    28
    ·
    1 year ago

    Just do a while loop and subtract 2 if it’s positive or plus 2 is it’s negative until it reaches 1 or 0 and that’s how you know, easy! /s

    • KoboldCoterie@pawb.social
      link
      fedilink
      English
      arrow-up
      31
      ·
      1 year ago

      God, it’s so obvious, you can do it in only two lines of code.

      if (number == 1 || number == 3 || number == 5 || number == 7 || number == 9...) return false;
      else return true;
      
  • Einar@lemm.ee
    link
    fedilink
    arrow-up
    11
    arrow-down
    4
    ·
    edit-2
    1 year ago

    Recently there was a thread trying to declare PHP obsolete.

    Hard to beat this in efficiency:

    function is_even($num) {
        return $num % 2 === 0;
    }
    
    

    That said, this should work similarly in most languages.

    • jmcs@discuss.tchncs.de
      link
      fedilink
      arrow-up
      5
      ·
      1 year ago

      If the language you are using uses “real” integers, using a bit mask to get the least significant bit is probably a lot faster - assuming the compiler doesn’t replace the operation for you, in which case it doesn’t matter.

  • Rentlar@lemmy.ca
    link
    fedilink
    arrow-up
    87
    ·
    edit-2
    1 year ago

    They call me a StackOverflow expert:

    private bool isEven(int num) {
    if (num == 0) return true;
    if (num == 1) return false;
    if (num < 0) return isEven(-1 * num);
    return isEven(num - 2);
    }
    
    • Johanno@feddit.de
      link
      fedilink
      arrow-up
      16
      ·
      edit-2
      1 year ago

      StackoverflowException.

      What do I do now?

      Nvm. Got it.

        if(num % 2 == 0){
             int num1 = num/2
             int num2 = num/2
             return isEven(num1) && isEven(num2)   
        } 
      
      if(num % 3 == 0){
            int num1 = num/3
            int num2 = num/3
            int num3 = num/3
            return isEven(num1) && isEven(num2) && isEven(num3) 
      }
      

      Obviously we need to check each part of the division to make sure if they are even or not. /s

  • rollerbang@sopuli.xyz
    link
    fedilink
    arrow-up
    32
    ·
    1 year ago

    You have to make it easy on yourself and just use a switch with default true for evens, then gandle all the odd numbers in individual cases. There, cut your workload in half.

  • enkers@sh.itjust.works
    link
    fedilink
    arrow-up
    29
    ·
    edit-2
    1 year ago

    This is your brain on python:

    def is_even (num):
         return num in [x*2 for x in range(sys.maxsize / 2)]
    
      • BeigeAgenda@lemmy.ca
        link
        fedilink
        arrow-up
        23
        arrow-down
        1
        ·
        edit-2
        1 year ago

        No its not the wrong solution! Premature optimization is a waste of time.

        Using if or case are not a solution because they are way too verbose and very easy to introduce an error.

        Modulo is a solution, and using bit-wise and is another faster solution.

        • mryessir@lemmy.sdf.org
          link
          fedilink
          arrow-up
          3
          arrow-down
          13
          ·
          1 year ago

          You call it premature optimization. I call it obvious.

          You use a flat head as a Phillip’s.

          • BeigeAgenda@lemmy.ca
            link
            fedilink
            arrow-up
            6
            ·
            1 year ago

            I call it making assumptions that may be incorrect, and do you know if the compiler will do the optimization anyway in this case?

            • mryessir@lemmy.sdf.org
              link
              fedilink
              arrow-up
              1
              arrow-down
              2
              ·
              edit-2
              1 year ago

              What statement do you flag as assumption? Yes, I do. The modulo operator is only a subset of bit masks. It is more explicit to write:

              if ( (variable &EVEN_MASK) == 0) …

              To act upon even numbers then:

              if ( (variable %2) == 0) …

              How would you name the 2 in the above statement for more expressive power?

              EVEN_MODULO_OP ? That may throw more people off imo.

              • BeigeAgenda@lemmy.ca
                link
                fedilink
                arrow-up
                2
                arrow-down
                3
                ·
                1 year ago

                I give up, I was wrong to even think about the modulo operator, you are clearly the master programmer. 🥇

                This reminds me of a discussion about the ternary operator ? :, some people think its the one true way of writing code because its just so clear what it does. And I say please use it sparingly because if you start doing nested ternary operators its very hard to unpack what your code does, and I prefer readability over compact code, especially with today’s compilers.

              • Kogasa@programming.dev
                link
                fedilink
                arrow-up
                8
                ·
                1 year ago

                You shouldn’t rename 2 at all. “Even” has a commonly understood meaning that is instantly recognizable from (variable %2) == 0. The bitmask is an overgeneralization.

  • Scubus@sh.itjust.works
    link
    fedilink
    arrow-up
    11
    ·
    1 year ago

    string taco = variable.ToString()[variable.ToString().Length - 1];

    If (taco == “0” || taco == “2” || taco == “4” || taco == “6” || taco == “8”)

    return true;

    else

    return false;

    Im something of a coding master myself

    • elauso@feddit.de
      link
      fedilink
      arrow-up
      3
      ·
      1 year ago

      Yeah, “just use modulo” - no shit, you must be some kind of master programmer