fork download
  1. open System
  2. open System.Collections.Generic
  3.  
  4. let recip =
  5. let cache = Dictionary<_, _>()
  6. fun n m ->
  7. if cache.ContainsKey( (n, m) ) then cache.[ (n, m) ]
  8. else
  9. let res = List.find (fun a -> n * a % m = 1) [1..n]
  10. cache.[ (n, m) ] <- res
  11. res
  12.  
  13. let rec binomialMod n k m =
  14. if k = 1 then n % m
  15. else
  16. let prev = binomialMod (n - 1) (k - 1) m
  17. prev * n * (recip k m) % m
  18.  
  19. System.Console.WriteLine (recip 5 1007)
  20.  
Runtime error #stdin #stdout 0.13s 15544KB
stdin
1
2
10
42
11
stdout
Standard output is empty