Created
November 19, 2022 12:40
-
-
Save Hermann-SW/6379e236f7c48f2eefe26d83e62b0cea to your computer and use it in GitHub Desktop.
fac(n) computes factorial, facmod(n, m) is faster in reducing all recursive results "mod m"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define fac(n) { | |
if (n==0) return 1; | |
return n*fac(n-1) | |
} | |
define facmod(n, m) { | |
if (n==0) return 1; | |
return n*fac(n-1)%m | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wilson's theorem:
(n-1)! ≡ -1 (mod n) ⇔ n is prime
https://en.wikipedia.org/wiki/Wilson%27s_theorem
Only for n starting at 30,000 "fac(n)%m" becomes slower than "facmod(n, m)":