Jacobi symbol: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: Disaggregated the tabulation functions a little)
Line 288: Line 288:
p = (3 ==) . flip rem 4
p = (3 ==) . flip rem 4
r = rem x y
r = rem x y

------------------------- DISPLAY -------------------------

jacobiTable :: Int -> Int -> String
jacobiTable nCols nRows =
withColumnLabels ("" : fmap show [0 .. nCols]) $
labelledRows (fmap show [1,3 .. (2 * nRows)]) $
paddedCols $
chunksOf nRows $
uncurry jacobi <$> ((,) <$> [0 .. pred nCols] <*> [1,3 .. (nRows * 2)])



-------------------------- TEST ---------------------------
-------------------------- TEST ---------------------------
Line 293: Line 304:
main = putStrLn $ jacobiTable 11 9
main = putStrLn $ jacobiTable 11 9



------------------------- DISPLAY -------------------------

jacobiTable :: Int -> Int -> String
------------------ TABULATION FUNCTIONS -------------------
jacobiTable nCols nRows =

let nth = (nRows * 2)
ys = [1,3 .. nth]
paddedCols :: Show a => [[a]] -> [[String]]
paddedCols cols =
w = length (show nth)
let scols = fmap show <$> cols
rjust = justifyRight w ' ' . show
showRow (x:xs) = unwords $ rjust x : "->" : map rjust xs
w = maximum $ length <$> concat scols
in map (justifyRight w ' ') <$> scols
cols = [0 .. pred nCols]

rowLines =
labelledRows :: [String] -> [[String]] -> [[String]]
showRow <$>
labelledRows labels cols =
transpose
(ys :
let w = maximum $ map length labels
in zipWith (:) ((++ " ->") . justifyRight w ' ' <$> labels) (transpose cols)
chunksOf

nRows
withColumnLabels :: [String] -> [[String]] -> String
(uncurry jacobi <$> ((<*>) . fmap (,)) cols [1,3 .. (nRows * 2)]))
withColumnLabels labels rows@(x:_) =
in unlines $
(justifyRight (4 + w) ' ' [] ++ unwords (rjust <$> cols)) :
let labelRow = unwords $ zipWith (`justifyRight` ' ') (length <$> x) labels
replicate (length $ head rowLines) '-' : rowLines
in unlines $ labelRow : replicate (length labelRow) '-' : fmap unwords rows


justifyRight :: Int -> a -> [a] -> [a]
justifyRight :: Int -> a -> [a] -> [a]