let resolution = 5000 let iters1 max_iter xc yc = let rec aux count x y = if count = max_iter then max_iter else begin if x *. x +. y *. y >= 4.0 then count else aux (count+1) (x *. x -. y *. y +. xc) (2.0 *. x *. y +. yc) end in aux 0 xc yc let run iters = let st = Unix.gettimeofday () in let max_val = resolution/2 in let min_val = - max_val in let mul = 2.0 /. float_of_int max_val in let count = ref 0 in for i=min_val to max_val do let x = float_of_int i *. mul in for j = min_val to max_val do let y = float_of_int j *. mul in count := !count + iters 100 x y; done done; let en = Unix.gettimeofday () in Printf.printf "result: %d\nTime: %g\n" !count (en -. st); flush stdout type complex = { mutable x : float; mutable y : float; } let norm_square c = c.x *. c.x +. c.y *. c.y let mul c1 c2 = { x = c1.x *. c2.x -. c1.y *. c2.y; y = c1.x *. c2.y +. c1.y *. c2.x } let add c1 c2 = { x = c1.x +. c2.x; y = c1.y +. c2.y } let iters2 max_iter xc yc = let c = { x = xc; y = yc } in let rec aux count z = if count >= max_iter then max_iter else begin if norm_square z >= 4.0 then count else begin aux (count+1) (add (mul z z) c) end end in aux 0 c class complex_c x_init y_init = object val x = x_init val y = y_init method get_x = x method get_y = y method norm_square = x *. x +. y *. y method add (c:complex_c) = new complex_c (x +. c#get_x) (y +. c#get_y) method mul (c:complex_c) = new complex_c (x *. c#get_x -. y *. c#get_y) (x *. c#get_y +. y *. c#get_x) end let iters3 max_iter xc yc = let c = new complex_c xc yc in let rec aux count z = if count >= max_iter then max_iter else begin if z#norm_square >= 4.0 then count else begin aux (count+1) ((z#mul z)#add c) end end in aux 0 c let _ = run iters1; run iters2; run iters3