fork download
  1. open System
  2. open System.Collections.Generic
  3.  
  4. let recip =
  5. let cache = Dictionary<_, _>()
  6. fun n m ->
  7. assert (isPrime n)
  8. if cache.ContainsKey( (n, m) ) then cache.[ (n, m) ]
  9. else
  10. let res = List.find (fun a -> n * a % m = 1) [1..(m - 1)]
  11. cache.[ (n, m) ] <- res
  12. res
  13.  
  14. let rec binomialMod n k m =
  15. let rec recBinomialMod n k m result =
  16. if k = 1 then n * result % m
  17. else
  18. let k_1 = recip k m
  19. recBinomialMod (n - 1) (k - 1) m (result * n * k_1 % m)
  20. recBinomialMod n k m 1
  21.  
  22. let isPrime n =
  23. List.exists (fun i -> n % i = 0) [2..(n - 1)]
  24.  
  25. let primes = List.filter isPrime [1000..5000]
  26. let binomials = List.map (fun m -> binomialMod (int (1000000000000L % (int64 m))) 1000000000 m) primes
  27.  
  28. let solution =
  29. let rec recSolution (binomials:(int list)) product chosen =
  30. seq {
  31. if chosen = 3 then yield product
  32. else if binomials.IsEmpty then yield 0
  33. else
  34. yield! recSolution binomials.Tail product chosen
  35. yield! recSolution binomials.Tail (product * binomials.Head) (chosen + 1)
  36. }
  37. (Seq.sum (recSolution binomials 1 0))
  38.  
  39. Console.WriteLine solution
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
1
2
10
42
11
compilation info
/home/3oswFw/prog.fs(7,15): error FS0039: The value or constructor 'isPrime' is not defined
stdout
Standard output is empty