CLI interface + restructuration + precision fixes Fermat Factorization
This commit is contained in:
36
app/FactorizationUI.hs
Normal file
36
app/FactorizationUI.hs
Normal file
@@ -0,0 +1,36 @@
|
||||
module FactorizationUI (run) where
|
||||
|
||||
import Utils (askChoice, askNumber)
|
||||
import Factorization (fermatFactorization, pollardPminus1)
|
||||
|
||||
run :: IO ()
|
||||
run = do
|
||||
putStrLn "Factorization"
|
||||
putStrLn "1) Fermat factorization"
|
||||
putStrLn "2) Pollard p-1"
|
||||
|
||||
choice <- askChoice 2
|
||||
|
||||
case choice of
|
||||
1 -> fermatUI
|
||||
2 -> pollardUI
|
||||
_ -> error "Impossible"
|
||||
|
||||
fermatUI :: IO ()
|
||||
fermatUI = do
|
||||
n <- askNumber "Enter an integer n > 1:"
|
||||
let (p, q) = fermatFactorization n
|
||||
putStrLn ("n = " ++ show n)
|
||||
putStrLn ("p = " ++ show p)
|
||||
putStrLn ("q = " ++ show q)
|
||||
|
||||
pollardUI :: IO ()
|
||||
pollardUI = do
|
||||
n <- askNumber "Enter an integer n > 1:"
|
||||
b <- askNumber "Enter the bound B:"
|
||||
case pollardPminus1 n b of
|
||||
Just factor -> do
|
||||
putStrLn ("Found factor: " ++ show factor)
|
||||
putStrLn ("Other factor: " ++ show (n `div` factor))
|
||||
Nothing ->
|
||||
putStrLn "Failed to find a factor (try a different B or method)."
|
||||
16
app/Main.hs
16
app/Main.hs
@@ -1,7 +1,19 @@
|
||||
module Main where
|
||||
|
||||
import qualified Primes ()
|
||||
import Utils (askChoice)
|
||||
import qualified FactorizationUI
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
putStrLn "unimplemented"
|
||||
putStrLn "Haskell Math Toolkit"
|
||||
putStrLn "1) Factorization"
|
||||
putStrLn "2) Modular square root (not yet implemented)"
|
||||
putStrLn "3) Primality test (not yet implemented)"
|
||||
|
||||
choice <- askChoice 3
|
||||
|
||||
case choice of
|
||||
1 -> FactorizationUI.run
|
||||
2 -> putStrLn "Modular square root: not implemented yet."
|
||||
3 -> putStrLn "Primality test: not implemented yet."
|
||||
_ -> error "Impossible"
|
||||
|
||||
25
app/Utils.hs
Normal file
25
app/Utils.hs
Normal file
@@ -0,0 +1,25 @@
|
||||
module Utils (askNumber, askChoice) where
|
||||
|
||||
import Text.Read (readMaybe)
|
||||
import System.Exit (exitSuccess)
|
||||
|
||||
-- Ask user for an integer > 1, or exit on invalid input
|
||||
askNumber :: String -> IO Integer
|
||||
askNumber s = do
|
||||
putStrLn s
|
||||
input <- getLine
|
||||
case readMaybe input of
|
||||
Just n | n > 1 -> return n
|
||||
_ -> do
|
||||
putStrLn "Not a valid integer"
|
||||
exitSuccess
|
||||
|
||||
askChoice :: Int -> IO Int
|
||||
askChoice maxChoice = do
|
||||
putStrLn "Enter your choice:"
|
||||
input <- getLine
|
||||
case readMaybe input of
|
||||
Just n | n >= 1 && n <= maxChoice -> return n
|
||||
_ -> do
|
||||
putStrLn "Invalid choice"
|
||||
exitSuccess
|
||||
Reference in New Issue
Block a user