• katy ✨@piefed.blahaj.zone
    link
    fedilink
    English
    arrow-up
    55
    ·
    edit-2
    2 个月前
    bool isOdd(int num) {  
    	const oddNumbers = [];  
    	for (let i = 1; i <= 10000000; i += 2) {  
      		oddNumbers.push(i);  
    	}  
    	if (oddNumbers.includes(num) {  
    		return true;  
    	}  
    }  
    
      • Mad_Punda@feddit.org
        link
        fedilink
        arrow-up
        24
        ·
        edit-2
        2 个月前

        Might very well be an endless loop because tail recursion can be optimized to reuse the stack frame. Depends on a lot of things of course.

        • orhtej2@eviltoast.orgOP
          link
          fedilink
          English
          arrow-up
          22
          ·
          2 个月前

          Forkbomb kills the entire system so not really.

          With the stack overflow the runtime will gracefully terminate the program.

        • calcopiritus@lemmy.world
          link
          fedilink
          arrow-up
          12
          ·
          edit-2
          2 个月前

          No.

          A stack overflow is a symptom, not the illness. A fork bomb is an illness.

          Software coming from the mathematical point of view, assummes it has infinite resources. However, a real computer has many resources that are finite.

          CPU time is finite. Memory amount is finite. There is a finite number of network ports. And so on.

          A stack overflow just means: “you have run out of this resource called ‘the stack’”. The stack is a region of the memory. Each thread of each process has 1 stack, and it is not infinite in size. This program will cause a stack overflow because it is infinitely recursive, and each function call will consume a bit of the stack.

          A forkbomb is not the end of a finite resource. A fork bomb is a program that uses “forking” to rapidly consume system resources. A fork bomb might cause a stack overflow. Or an out of memory issue. Slow the computer a lot. Or if the OS has a hard limit for process amount, it might reach that limit.

          • davidgro@lemmy.world
            link
            fedilink
            arrow-up
            1
            ·
            2 个月前

            A program such as the one in this post is a loop designed (intentionally or not) to run out of stack regardless of how much there is. I’d call that an illness rather than a symptom.

    • Valmond@lemmy.world
      link
      fedilink
      arrow-up
      2
      ·
      2 个月前

      Program it with template meta programming and cause a stack overflow when compiling 🤓😎

  • OpenStars@piefed.social
    link
    fedilink
    English
    arrow-up
    23
    ·
    2 个月前

    Boss: don’t spend any time on it, just vibe code a solution.

    You: sure, I enjoy receiving a salary, what could go wrong?

    • Decq@lemmy.world
      link
      fedilink
      arrow-up
      11
      arrow-down
      1
      ·
      edit-2
      2 个月前

      To be fair in a dynamic typed language with dumb string to int coercions, I kinda get why such a library would exists. So it’s more a symptom of terrible language design than modern dependency hell.

      • bobo@lemmy.ml
        link
        fedilink
        arrow-up
        9
        ·
        edit-2
        2 个月前

        in a dynamic typed language with dumb string to int coercions, I kinda get why such a would library exists.

        If string return nan, else % 2

        So it’s more a symptom of terrible language design than modern dependency hell.

        Dependency chain: is-even depends on is-odd which depends on is-number

        • Decq@lemmy.world
          link
          fedilink
          arrow-up
          2
          arrow-down
          1
          ·
          edit-2
          2 个月前

          If string return nan, else % 2

          So now you return a number type if it’s a string and a boolean if it’s an integer. How does that make sense?

          The is-even lib exists to sanitize input by throwing an exception which imho is better.

          Edit: having looked at the code better. Apparently it still allows string coercion (boo). It only checks for non integer numbers.

  • cogman@lemmy.world
    link
    fedilink
    arrow-up
    7
    ·
    edit-2
    2 个月前

    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
      ·
      2 个月前

      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
      ·
      2 个月前

      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
        ·
        2 个月前

        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.

      • killingspark@feddit.org
        link
        fedilink
        English
        arrow-up
        3
        ·
        2 个月前

        Nah, tail recursion optimization can just reuse the same stack frame again and again. It’s going to loop until it wraps around what ever integer width it has and then tells you if the biggest integer is even or odd. Or, if it’s nice, it’s going to complain about the wrap around

  • humanspiral@lemmy.ca
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    2 个月前

    easy fix… if infinity return false.

    mathematical breakthrough bonus proof: all numbers are neither even nor odd.