• cogman@lemmy.world
    link
    fedilink
    arrow-up
    7
    ·
    edit-2
    18 hours ago

    Fixed

    boolean isOdd(int num) {
      if(num == 1)
        return true;
      if(num > 0)
        return isEven(num - 1);
      else
        return isEven(num + 1);
    }
    
    boolean isEven(int num) {
      if(num > 0)
        return isOdd(num - 1);
      else
        return isOdd(num + 1);
    }
    
    • affiliate@lemmy.world
      link
      fedilink
      arrow-up
      11
      ·
      17 hours ago

      the downside with this approach is that it will eventually terminate. the version in the original post has the advantage of giving me plenty of time to contemplate life’s many mysteries.

    • Rednax@lemmy.world
      link
      fedilink
      arrow-up
      3
      arrow-down
      2
      ·
      17 hours ago

      Why the complicated if statements to check the sign? Just let the number overflow. Would be functionaly the same, and result in much prettier code.

      • cogman@lemmy.world
        link
        fedilink
        arrow-up
        6
        ·
        17 hours ago

        That’s a platform dependent change. Overflow is undefined behavior. I’d rather have my code portable so it can run on my Univac 1101.