• Ephera
    link
    fedilink
    English
    arrow-up
    8
    ·
    10 months ago

    For large numbers, you might get a stack overflow. Depends on whether the compiler recognizes that it’s a tail recursion and then handles it appropriately.

    As for why Lemmy ignores single newlines, that’s because it uses Markdown for its comment syntax, which happens to handle newlines that way. If you want a single newline to display, you have to add two spaces at the end of the line.

    Here, you really would have wanted to use a code block, though, which you can create with backticks

    ```
    like
    so.
    ```

    Normally, this would display “like so.” in a monospace font and handle newlines as you expected:

    like
    so.
    
    • Buddahriffic@lemmy.world
      link
      fedilink
      arrow-up
      9
      ·
      edit-2
      10 months ago

      Just need to add some code that saves the base stack pointer, detects when the stack is about to run out, then saves the current number, resets the stack to the original pointer but now with the current number as x and continues on to fix the stack overflow issue. You don’t even have to restore the stack when it’s all unwinding!

      The integer overflow issue would be more complicated to solve except it isn’t really an issue because chopping off the upper bits doesn’t affect the eveness of a value.

      Oh and I thought of a quick way to verify the result of the algorithm. It should give the same result as this expression that you can use in your test suite:

      !(x & 1)
      

      Edit: um, new formatting problem… And that amp shows up in the edit box so it might even keep expanding.

      • Ephera
        link
        fedilink
        English
        arrow-up
        8
        ·
        10 months ago

        The ampersand thing is a known bug in Lemmy. It has to do with input sanitation, as I understand.