Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- exception Break;;
- let sqrt_int x = int_of_float(sqrt(float_of_int x));;
- let isPrime x =
- try
- for i=2 to (sqrt_int x) do
- if (x mod i) = 0 then
- raise Break;
- done;
- true;
- with
- Break -> false
- ;;
- let rec primes x =
- let rec loop x i =
- if i = x then
- [x]
- else
- if x mod i = 0 then
- if (isPrime i) then
- i::(primes (x/i))
- else
- (primes i)@(primes (x/i))
- else
- loop x (i+1)
- in
- loop x 2
- ;;
- List.iter (fun x-> print_int x; print_string " ") (primes 18);;
- (*
- output:
- eni@rocket:~/Documents/code/ocaml$ ocaml primes.ml
- 2 3 3
- *)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement