From 2e0cbfe714ef130cc51289228a4ed76d3de385bb Mon Sep 17 00:00:00 2001 From: Sam HADOW Date: Mon, 12 Jan 2026 13:33:29 +0100 Subject: [PATCH] primality tests UI --- app/Main.hs | 5 +++-- app/PrimesUI.hs | 29 +++++++++++++++++++++++++++++ haskell-math.cabal | 1 + 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 app/PrimesUI.hs diff --git a/app/Main.hs b/app/Main.hs index aaf8df9..76a68fa 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -2,18 +2,19 @@ module Main where import Utils (askChoice) import qualified FactorizationUI +import qualified PrimesUI main :: IO () main = do putStrLn "Haskell Math Toolkit" putStrLn "1) Factorization" putStrLn "2) Modular square root (not yet implemented)" - putStrLn "3) Primality test (not yet implemented)" + putStrLn "3) Primality tests" choice <- askChoice 3 case choice of 1 -> FactorizationUI.run 2 -> putStrLn "Modular square root: not implemented yet." - 3 -> putStrLn "Primality test: not implemented yet." + 3 -> PrimesUI.run _ -> error "Impossible" diff --git a/app/PrimesUI.hs b/app/PrimesUI.hs new file mode 100644 index 0000000..cf7db2b --- /dev/null +++ b/app/PrimesUI.hs @@ -0,0 +1,29 @@ +module PrimesUI (run) where + +import Utils (askChoice, askNumber) +import Primes (fermatPrimeTest, millerRabin, solovayStrassen) + +run :: IO () +run = do + putStrLn "Primality tests" + putStrLn "1) Fermat primality test" + putStrLn "2) Miller–Rabin test" + putStrLn "3) Solovay–Strassen test" + + choice <- askChoice 3 + + n <- askNumber "Enter integer n > 1 to test for primality:" + k <- askNumber "Enter security parameter k (number of rounds):" + + result <- case choice of + 1 -> fermatPrimeTest n k + 2 -> millerRabin n k + 3 -> solovayStrassen n k + _ -> error "Impossible choice" + + putStrLn ("n = " ++ show n) + putStrLn ("k = " ++ show k) + + if result + then putStrLn "Result: probably prime." + else putStrLn "Result: composite." diff --git a/haskell-math.cabal b/haskell-math.cabal index 8e7bb03..32c6a5f 100644 --- a/haskell-math.cabal +++ b/haskell-math.cabal @@ -41,6 +41,7 @@ executable haskell-math main-is: Main.hs other-modules: FactorizationUI + PrimesUI Utils build-depends: base ^>=4.18.2.1,