modular square root UI
This commit is contained in:
@@ -2,19 +2,20 @@ module Main where
|
|||||||
|
|
||||||
import Utils (askChoice)
|
import Utils (askChoice)
|
||||||
import qualified FactorizationUI
|
import qualified FactorizationUI
|
||||||
|
import qualified ModularSquareRootUI
|
||||||
import qualified PrimesUI
|
import qualified PrimesUI
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
putStrLn "Haskell Math Toolkit"
|
putStrLn "Haskell Math Toolkit"
|
||||||
putStrLn "1) Factorization"
|
putStrLn "1) Factorization"
|
||||||
putStrLn "2) Modular square root (not yet implemented)"
|
putStrLn "2) Modular square root"
|
||||||
putStrLn "3) Primality tests"
|
putStrLn "3) Primality tests"
|
||||||
|
|
||||||
choice <- askChoice 3
|
choice <- askChoice 3
|
||||||
|
|
||||||
case choice of
|
case choice of
|
||||||
1 -> FactorizationUI.run
|
1 -> FactorizationUI.run
|
||||||
2 -> putStrLn "Modular square root: not implemented yet."
|
2 -> ModularSquareRootUI.run
|
||||||
3 -> PrimesUI.run
|
3 -> PrimesUI.run
|
||||||
_ -> error "Impossible"
|
_ -> error "Impossible"
|
||||||
|
|||||||
20
app/ModularSquareRootUI.hs
Normal file
20
app/ModularSquareRootUI.hs
Normal file
@@ -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."
|
||||||
@@ -42,6 +42,7 @@ executable haskell-math
|
|||||||
other-modules:
|
other-modules:
|
||||||
FactorizationUI
|
FactorizationUI
|
||||||
PrimesUI
|
PrimesUI
|
||||||
|
ModularSquareRootUI
|
||||||
Utils
|
Utils
|
||||||
build-depends:
|
build-depends:
|
||||||
base ^>=4.18.2.1,
|
base ^>=4.18.2.1,
|
||||||
|
|||||||
Reference in New Issue
Block a user