List Info

Thread: "ocaml_beginners"::[] Fast IO




"ocaml_beginners"::[] Fast IO
user name
2006-12-17 05:47:12

problem specs:
http://www.spoj.pl/problems/INTEST/

caml results:
http://www.spoj.pl/ranks/INTEST/lang=CAML

I cannot for the life of me see how I can speed up my code so that it
runs in 1.6 seconds rather than 4.58. I've tried functional and
imperative implementations but they scored exactly the same.

imperative code:

let (n,k) = Scanf.scanf "%i %in" (fun a b -> (a,b));;

let counter = ref 0;;

for i=1 to n do
if read_int () mod k = 0 then
incr counter;
done;;

print_int !counter;;

exit 0;;

functional code:

let (n,k) = Scanf.scanf "%i %in" (fun a b -> (a,b));;

let rec count counter = function
0 -> counter
| x -> if (read_int ()) mod k = 0 then count (counter+1) (x-1) else
count counter (x-1);;

print_int (count 0 n);;

exit 0;;

__._,_.___
.

__,_._,___
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )