Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let rec eratosthene n =
- if n < 2 then
- [ ]
- else
- let primes = eratosthene ( n - 1 ) in
- let rec is_prime primes n =
- match primes with
- | [ ] -> true
- | head :: tail -> n mod head <> 0 && is_prime tail n
- in
- if is_prime primes n then
- primes @ [ n ] (* I don't know if I should reverse the list or not... because appending would be faster but 2 divides more numbers than 3 etc. so it would take longer to find out a number isn't a prime... *)
- else
- primes
- ;;
- let rec print_list l =
- match l with
- | [ ] -> ( )
- | head :: tail -> begin
- print_int head;
- print_newline ( );
- print_list tail
- end
- ;;
- print_list ( eratosthene 100 );;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement