Sha256: 51632c3d934ce58e51437cf45450349a19195b7ee4a84a2615e2532f64902743

Contents?: true

Size: 667 Bytes

Versions: 42

Compression:

Stored size: 667 Bytes

Contents

open Base

let square_root_trunc n =
    (* Square root is not defined on int64 *)
    Int64.to_float n |> Float.sqrt |> Int64.of_float

let rec factors_of' = function
    | 1L -> []
    | n  -> factors_of_loop n
and factors_of_loop n =
    let open Int64 in
    let i = ref (square_root_trunc n) in
    let res = ref None in
    while !i > 1L && Option.is_none !res do
        let i' = !i in
        if (rem n i') = 0L then (
            let m = n / i' in
            res := Some (List.concat [factors_of' i'; factors_of' m]));
        i := (i' - 1L);
    done;
    Option.value ~default:[n] !res

let factors_of n = List.sort ~compare:Int64.compare (factors_of' n)

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.179 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.178 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.177 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.176 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.175 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.174 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.173 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.172 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.171 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.170 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.169 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.167 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.166 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.165 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.164 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.163 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.162 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.161 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.160 tracks/ocaml/exercises/prime-factors/example.ml