Remember your assembly program that could factor one quadrillion in 6 seconds? I made a O'Caml program. On my machine it takes 12 seconds (better than Ruby and Python who took a couple of minutes). Try it out.
to compile:
And here's the actual code:Code:% ocamlopt nums.cmxa factor.ml -o factor
I should try with 64-bits integers to see if I get a speed improvement.Code:open Big_int;; let rec get_factors n sqroot diviser = if (le_big_int diviser sqroot) then begin if (eq_big_int (mod_big_int n diviser) zero_big_int) then begin Printf.printf "%s and %s\n" (string_of_big_int diviser) (string_of_big_int (div_big_int n diviser)); flush stdout; end; get_factors n sqroot (add_big_int diviser unit_big_int); end let _ = if (Array.length Sys.argv) < 2 then begin Printf.printf "Usage: %s <Integer>\n" Sys.argv.(0); exit 1; end; let n = big_int_of_string Sys.argv.(1) in let sqroot = sqrt_big_int n in get_factors n sqroot unit_big_int


Reply With Quote
Bookmarks