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..(m - 1)]
  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. let isPrime n =
  20. List.exists (fun i -> n % i = 0) [2..(n - 1)]
  21.  
  22. let primes = List.filter isPrime [1000..5000]
  23. let binomials = List.map (fun m -> binomialMod (int (1000000000000L % (int64 m))) 1000000000 m) primes
  24.  
  25. let solution =
  26. let rec recSolution (binomials:(int list)) product chosen =
  27. seq {
  28. if binomials.IsEmpty then yield 0
  29. else if chosen = 3 then yield product
  30. else
  31. yield! recSolution binomials.Tail product chosen
  32. yield! recSolution binomials.Tail (product * binomials.Head) (chosen + 1)
  33. }
  34. (Seq.sum (recSolution binomials 1 0))
  35.  
  36. Console.WriteLine solution
  37.  
Runtime error #stdin #stdout 2.69s 23520KB
stdin
1
2
10
42
11
stdout
Standard output is empty