- Commit
- 226f7ed16ab40b054da966dc410d74ce56de5933
- Parent
- 19e1ac07e6180cede53110fea0c61e125a961860
- Author
- Pablo Escobar Gaviria <gark.garcia@protonmail.com>
- Date
Formatted the OCaml implementation.
An exercise on polyglossy: the same problem solved on multiple languages
Formatted the OCaml implementation.
1 file changed, 32 insertions, 52 deletions
Status | File Name | N° Changes | Insertions | Deletions |
Modified | OCaml/main.ml | 84 | 32 | 52 |
diff --git a/OCaml/main.ml b/OCaml/main.ml @@ -4,17 +4,8 @@ For all A and B in N, S(A + B) = S(A) + S(B) - 9k, where k is an interger. *) -let failure: int = 1;; -let invalid_input: int = 2;; - -type range = - { sums_cache: int array; - start: int; - step: int; - stop: int; - channel: bool Event.channel - } -;; +let failure: int = 1 +let invalid_input: int = 2 (** Returns the sum of the digits of `n`, where `n` is a positive integer. *) let sum_digits (n: int) : int = @@ -22,67 +13,56 @@ let sum_digits (n: int) : int = match n with | 0 -> acc | _ -> sum_digits_tail (n / 10) (acc + n mod 10) - in - sum_digits_tail n 0 -;; + in sum_digits_tail n 0 (** Precompute the values of `sum_digits`.*) let get_sums (max: int) : int array = Array.init (2 * max + 1) sum_digits -;; let test (a: int) (b: int) (sums_cache: int array) : bool = let sum_digits n = Array.get sums_cache n in 0 <> (sum_digits (a + b) - sum_digits a - sum_digits b) mod 9 -;; let rec listen (c: bool Event.channel) (n: int) : unit = match n with | 0 -> () - | _ when Event.sync (Event.receive c) -> exit failure - | _ -> listen c (n - 1) -;; - -let counterexempl_range (r: range): unit = - let send b = let _ = Event.send r.channel b in Thread.exit () - and a = ref r.start in - while !a <= r.stop do - for b = 0 to !a do - if test !a b r.sums_cache then send true - done; - - a := !a + r.step - done; + | _ -> + if Event.sync (Event.receive c) then exit failure + else listen c (n - 1) - send false -;; - -(* TODO: Use concurency. *) let counterexempl (max: int) (n_threads: int) : unit = let sums_cache = get_sums max in let c = Event.new_channel () in - let range_of n = - { sums_cache = sums_cache; - start = n; - step = n_threads; - stop = max; - channel = c - } in - let spawn n = Thread.create counterexempl_range (range_of n) in + let counterexempl_range start = + let send b = let _ = Event.send c b in Thread.exit () + and a = ref start in + + while !a <= max do + for b = 0 to !a do + if test !a b sums_cache then send true + done; + + a := !a + n_threads + done; + send false in + let spawn = Thread.create counterexempl_range in + let _ = Array.init n_threads spawn in listen c n_threads -;; let main () = - if Array.length Sys.argv > 1 then - let max_str = Sys.argv.(1) in - - match int_of_string_opt max_str with - | Some max when max > 0 -> counterexempl max 2 - | _ -> exit invalid_input - else - exit invalid_input -;; + let arg n = + if Array.length Sys.argv > n then int_of_string_opt Sys.argv.(n) + else None in + let max = arg 1 + and n_threads = + match arg 2 with + | Some n -> n + | None -> 1 in + + match max with + | Some max when max > 0 && n_threads > 0 -> counterexempl max 2 + | _ -> exit invalid_input let () = main ()