Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
14
Task/Jacobi-symbol/Haskell/jacobi-symbol-1.hs
Normal file
14
Task/Jacobi-symbol/Haskell/jacobi-symbol-1.hs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
jacobi :: Integer -> Integer -> Integer
|
||||
jacobi 0 1 = 1
|
||||
jacobi 0 _ = 0
|
||||
jacobi a n =
|
||||
let a_mod_n = rem a n
|
||||
in if even a_mod_n
|
||||
then case rem n 8 of
|
||||
1 -> jacobi (div a_mod_n 2) n
|
||||
3 -> negate $ jacobi (div a_mod_n 2) n
|
||||
5 -> negate $ jacobi (div a_mod_n 2) n
|
||||
7 -> jacobi (div a_mod_n 2) n
|
||||
else if rem a_mod_n 4 == 3 && rem n 4 == 3
|
||||
then negate $ jacobi n a_mod_n
|
||||
else jacobi n a_mod_n
|
||||
72
Task/Jacobi-symbol/Haskell/jacobi-symbol-2.hs
Normal file
72
Task/Jacobi-symbol/Haskell/jacobi-symbol-2.hs
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import Data.Bool (bool)
|
||||
import Data.List (replicate, transpose)
|
||||
import Data.List.Split (chunksOf)
|
||||
|
||||
---------------------- JACOBI SYMBOL ---------------------
|
||||
|
||||
jacobi :: Int -> Int -> Int
|
||||
jacobi = go
|
||||
where
|
||||
go 0 1 = 1
|
||||
go 0 _ = 0
|
||||
go x y
|
||||
| even r =
|
||||
plusMinus
|
||||
(rem y 8 `elem` [3, 5])
|
||||
(go (div r 2) y)
|
||||
| otherwise = plusMinus (p r && p y) (go y r)
|
||||
where
|
||||
plusMinus = bool id negate
|
||||
p = (3 ==) . flip rem 4
|
||||
r = rem x y
|
||||
|
||||
|
||||
--------------------------- TEST -------------------------
|
||||
main :: IO ()
|
||||
main = putStrLn $ jacobiTable 11 9
|
||||
|
||||
------------------------- DISPLAY ------------------------
|
||||
jacobiTable :: Int -> Int -> String
|
||||
jacobiTable nCols nRows =
|
||||
let rowLabels = [1, 3 .. (2 * nRows)]
|
||||
colLabels = [0 .. pred nCols]
|
||||
in withColumnLabels ("" : fmap show colLabels) $
|
||||
labelledRows (fmap show rowLabels) $
|
||||
paddedCols $
|
||||
chunksOf nRows $
|
||||
uncurry jacobi
|
||||
<$> ((,) <$> colLabels <*> rowLabels)
|
||||
|
||||
------------------- TABULATION FUNCTIONS -----------------
|
||||
paddedCols ::
|
||||
Show a =>
|
||||
[[a]] ->
|
||||
[[String]]
|
||||
paddedCols cols =
|
||||
let scols = fmap show <$> cols
|
||||
w = maximum $ length <$> concat scols
|
||||
in map (justifyRight w ' ') <$> scols
|
||||
|
||||
labelledRows :: [String] -> [[String]] -> [[String]]
|
||||
labelledRows labels cols =
|
||||
let w = maximum $ map length labels
|
||||
in zipWith
|
||||
(:)
|
||||
((<> " ->") . justifyRight w ' ' <$> labels)
|
||||
(transpose cols)
|
||||
|
||||
withColumnLabels :: [String] -> [[String]] -> String
|
||||
withColumnLabels _ [] = ""
|
||||
withColumnLabels labels rows@(x : _) =
|
||||
let labelRow =
|
||||
unwords $
|
||||
zipWith
|
||||
(`justifyRight` ' ')
|
||||
(length <$> x)
|
||||
labels
|
||||
in unlines $
|
||||
labelRow :
|
||||
replicate (length labelRow) '-' : fmap unwords rows
|
||||
|
||||
justifyRight :: Int -> a -> [a] -> [a]
|
||||
justifyRight n c = (drop . length) <*> (replicate n c <>)
|
||||
Loading…
Add table
Add a link
Reference in a new issue