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) 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.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
1
2
10
42
11
compilation info
/home/1u3ZCh/prog.fs(32,15): error FS0001: This expression was expected to have type
    int    
but here has type
    seq<int>    

/home/1u3ZCh/prog.fs(34,23): error FS0001: Type mismatch. Expecting a
    int list    
but given a
    (int -> int) list    
The type 'int' does not match the type 'int -> int'
stdout
Standard output is empty