From a45f29178afd4d1d1336316ad86df6cb4e89664a Mon Sep 17 00:00:00 2001 From: Sam HADOW Date: Mon, 12 Jan 2026 13:47:05 +0100 Subject: [PATCH] modular square root UI --- app/Main.hs | 5 +++-- app/ModularSquareRootUI.hs | 20 ++++++++++++++++++++ haskell-math.cabal | 1 + 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 app/ModularSquareRootUI.hs diff --git a/app/Main.hs b/app/Main.hs index 76a68fa..adf224c 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -2,19 +2,20 @@ module Main where import Utils (askChoice) import qualified FactorizationUI +import qualified ModularSquareRootUI import qualified PrimesUI main :: IO () main = do putStrLn "Haskell Math Toolkit" putStrLn "1) Factorization" - putStrLn "2) Modular square root (not yet implemented)" + putStrLn "2) Modular square root" putStrLn "3) Primality tests" choice <- askChoice 3 case choice of 1 -> FactorizationUI.run - 2 -> putStrLn "Modular square root: not implemented yet." + 2 -> ModularSquareRootUI.run 3 -> PrimesUI.run _ -> error "Impossible" diff --git a/app/ModularSquareRootUI.hs b/app/ModularSquareRootUI.hs new file mode 100644 index 0000000..6e3d2df --- /dev/null +++ b/app/ModularSquareRootUI.hs @@ -0,0 +1,20 @@ +module ModularSquareRootUI (run) where + +import Utils (askNumber) +import ModularSquareRoot (tonelliShanks) + +run :: IO () +run = do + putStrLn "Modular square root (Tonelli–Shanks)" + putStrLn "Solve x^2 = n (mod p), with p an odd prime." + + n <- askNumber "Enter n:" + p <- askNumber "Enter prime modulus p:" + + case tonelliShanks n p of + Just x -> do + putStrLn "Solution found:" + putStrLn ("x = " ++ show x ++ " (mod " ++ show p ++ ")") + putStrLn ("Other root: " ++ show ((p - x) `mod` p)) + Nothing -> + putStrLn "No square root exists modulo p." diff --git a/haskell-math.cabal b/haskell-math.cabal index 32c6a5f..2e7a385 100644 --- a/haskell-math.cabal +++ b/haskell-math.cabal @@ -42,6 +42,7 @@ executable haskell-math other-modules: FactorizationUI PrimesUI + ModularSquareRootUI Utils build-depends: base ^>=4.18.2.1,