Jacobi symbol: Difference between revisions

Content added Content deleted
(→‎{{header|Haskell}}: Replaced a do with a (let ... in ...) (No monad used or needed here))
Line 275: Line 275:
import Data.List.Split (chunksOf)
import Data.List.Split (chunksOf)
import Data.Bool (bool)
import Data.Bool (bool)

jacobi :: Int -> Int -> Int
jacobi :: Int -> Int -> Int
jacobi = go
jacobi = go
Line 282: Line 282:
go 0 _ = 0
go 0 _ = 0
go x y
go x y
| even r = bool id negate (rem y 8 `elem` [3, 5]) (go (div r 2) y)
| even r = plusMinus (rem y 8 `elem` [3, 5]) (go (div r 2) y)
| otherwise = bool id negate (3 == rem r 4 && 3 == rem y 4) (go y r)
| otherwise = plusMinus (3 == rem r 4 && 3 == rem y 4) (go y r)
where
where
plusMinus = bool id negate
r = rem x y
r = rem x y

-------------------------- TEST ---------------------------
-------------------------- TEST ---------------------------
main :: IO ()
main :: IO ()
main = putStrLn $ jacobiTable 11 9
main = putStrLn $ jacobiTable 11 9

------------------------- DISPLAY -------------------------
------------------------- DISPLAY -------------------------
jacobiTable :: Int -> Int -> String
jacobiTable :: Int -> Int -> String
Line 310: Line 311:
(justifyRight w ' ' [] ++ " " ++ unwords (rjust <$> cols)) :
(justifyRight w ' ' [] ++ " " ++ unwords (rjust <$> cols)) :
replicate (length $ head rowLines) '-' : rowLines
replicate (length $ head rowLines) '-' : rowLines

justifyRight :: Int -> a -> [a] -> [a]
justifyRight :: Int -> a -> [a] -> [a]
justifyRight n c = (drop . length) <*> (replicate n c ++)</lang>
justifyRight n c = (drop . length) <*> (replicate n c ++)</lang>