Skip to content

Instantly share code, notes, and snippets.

@bcalmac
Created December 14, 2019 20:25
Show Gist options
  • Save bcalmac/fc183c244ab053c273c70c708f3da904 to your computer and use it in GitHub Desktop.
Save bcalmac/fc183c244ab053c273c70c708f3da904 to your computer and use it in GitHub Desktop.
Determine the list of remainders of powers of a divided by b
-- a^0 mod b is 1. Once we find the next power with a remainder of 1, the remainders will repeat.
-- Take from a list until the first element repeats
takeUntilFirstRepeats (x:xs) = x : takeWhile (/= x) xs
remaindersOfPowers a b = takeUntilFirstRepeats [a^i `mod` b | i <- [0..]]
-- Example
remaindersOfPowers 9 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment