Skip to content

Instantly share code, notes, and snippets.

@ConnorBaker
Last active September 21, 2020 02:24
Show Gist options
  • Select an option

  • Save ConnorBaker/7c886234fabd6498b3b257beb19c5ee4 to your computer and use it in GitHub Desktop.

Select an option

Save ConnorBaker/7c886234fabd6498b3b257beb19c5ee4 to your computer and use it in GitHub Desktop.
Find cyclic subgroups of finite order of groups with integer elements under modular multiplication
{-# LANGUAGE RecordWildCards #-}
module Main where
data Solution = Solution
{ remainder :: Integer
, element :: Integer
, power :: Integer
}
instance Show Solution where
show Solution{..} = (show remainder)
++ " = "
++ (show element)
++ "^{" ++ (show power) ++ "}"
cyclic :: (Integer -> Integer -> Integer) -- Group operation
-> Integer -- Identity of the group
-> Integer -- Element of group
-> [Solution]
cyclic op i e = map (uncurry (flip Solution e)) values
where
pows = iterate (\(rem, y) -> ((rem `op` e), y + 1)) (e, 1)
values = takeWhileInclusive ((i /=) . fst) pows
takeWhileInclusive :: (a -> Bool) -- Predicate
-> [a] -- Input
-> [a] -- Output
takeWhileInclusive p = foldr (\ x acc -> x : if p x then acc else []) []
main :: IO ()
main = do
putStrLn "hello world"
@ConnorBaker
Copy link
Copy Markdown
Author

ConnorBaker commented Sep 21, 2020

Updated so it takes an arbitrary function for the group.

In the first version, for U(14) you'd do

*Main> cyclic 3 14
[3 = 3^{1},9 = 3^{2},13 = 3^{3},11 = 3^{4},5 = 3^{5},1 = 3^{6}]

Now you do

*Main> cyclic (\ x y -> (x*y) `mod` 14) 1 3
[3 = 3^{1},9 = 3^{2},13 = 3^{3},11 = 3^{4},5 = 3^{5},1 = 3^{6}]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment