[Haskell-beginners] Prime number test performance on negative integers

Francesco Ariis fa-ml at ariis.it
Mon Dec 11 20:25:33 UTC 2023


Il 11 dicembre 2023 alle 21:41 Folsk Pratima ha scritto:
> Ahem,

Woops, I messed up that `otherwise`.

    import System.Environment

    isPrimeWiki :: Integer -> Bool
    isPrimeWiki 2 = True
    isPrimeWiki 3 = True
    isPrimeWiki n
        | n <= 1 ||
          rem n 2 == 0 ||
          rem n 3 == 0    = False
    isPrimeWiki n =
        let f i
              | i^2 > n = True
              | rem n i == 0 ||
                rem n (i+2) == 0 = False
              | otherwise = f (i+1)
        in f 5

    main :: IO ()
    main = do
        [n] <- getArgs
        print $ isPrimeWiki (read n)

Still, no sweat computing that:

    f at x270:/tmp/prova$ time ./prime 1506491439391566589
    False

    real    0m0.014s
    user    0m0.000s
    sys     0m0.005s



More information about the Beginners mailing list