Sha256: 65e7de429bb980865671fd8887997d358693503d7041fc22eab75095c28db78a

Contents?: true

Size: 714 Bytes

Versions: 138

Compression:

Stored size: 714 Bytes

Contents

open Core

let square_root_trunc n =
    (* Square root is not defined on int64 but it is on big_int *)
    let open Big_int in
    big_int_of_int64 n |> sqrt_big_int |> int64_of_big_int

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 ~cmp:Int64.compare (factors_of' n)

Version data entries

138 entries across 138 versions & 1 rubygems

Version Path
trackler-2.2.1.97 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.96 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.95 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.94 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.93 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.92 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.91 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.90 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.89 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.88 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.87 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.86 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.85 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.84 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.83 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.82 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.81 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.80 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.79 tracks/ocaml/exercises/prime-factors/example.ml
trackler-2.2.1.78 tracks/ocaml/exercises/prime-factors/example.ml