• JustARegularNerd@lemmy.dbzer0.com
    link
    fedilink
    arrow-up
    19
    arrow-down
    4
    ·
    17 hours ago

    I’m struggling to follow the code here. I’m guessing it’s C++ (which I’m very unfamiliar with)

    bool is_prime(int x) {
        return false;
    }
    

    Wouldn’t this just always return false regardless of x (which I presume is half the joke)? Why is it that when it’s tested up to 99999, it has a roughly 95% success rate then?

    • kraftpudding@lemmy.world
      link
      fedilink
      arrow-up
      34
      ·
      17 hours ago

      I suppose because about 5% of numbers are actually prime numbers, so false is not the output an algorithm checking for prime numbers should return

      • JustARegularNerd@lemmy.dbzer0.com
        link
        fedilink
        arrow-up
        11
        ·
        17 hours ago

        Oh I’m with you, the tests are precalculated and expect a true to return on something like 99991, this function as expected returns false, which throws the test into a fail.

        Thank you for that explanation

    • flamingo_pinyata@sopuli.xyz
      link
      fedilink
      arrow-up
      28
      ·
      17 hours ago

      That’s the joke. Stochastic means probabilistic. And this “algorithm” gives the correct answer for the vast majority of inputs