caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] A (much less) frustrated beginner
@ 2003-12-23 19:00 Tyler Eaves
  2003-12-23 21:13 ` Aleksey Nogin
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Tyler Eaves @ 2003-12-23 19:00 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 470 bytes --]

I think I'm beginning to get it. Attached find my second try at a prime
number generator.

This one is distinguished from my first by two important differences:

1: It is written (I think) in a completly functional style 
2: It actually *works*. It's pretty fast too. Running as an optimized
binary on my system (Athlon @ 950mhz under FreeBSD 5.1) it can calculate
the first 15104 primes (Highest is 165161) with one minute of CPU time.

Thanks again for all your help!

[-- Attachment #2: primes2.ml --]
[-- Type: text/plain, Size: 622 bytes --]

(*  primes2.ml 
    Prime number generator, take 2
    Tyler Eaves <tyler@ml1.net>
    *)
   
exception Not_prime;;
exception Its_Prime;;

let rec isprime n primes = (
    try
        List.iter (fun x -> if n mod x = 0 then raise Not_prime else if sqrt
        (float_of_int n) < (float_of_int x) then raise Its_Prime else ()) primes;
        Printf.printf "%d is PRIME!\n" n; 
        isprime (n+2) (List.concat [primes; [n]])
    with 
        Not_prime -> isprime (n+2) primes
        | Its_Prime -> (Printf.printf "%d is PRIME!\n" n; 
          isprime (n+2) (List.concat [primes; [n]]))
    );;

isprime 3 [2];;
    

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2003-12-24  8:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-12-23 19:00 [Caml-list] A (much less) frustrated beginner Tyler Eaves
2003-12-23 21:13 ` Aleksey Nogin
2003-12-24  1:04 ` Nicolas Cannasse
2003-12-24  6:23   ` Sven Luther
2003-12-24  6:42 ` james woodyatt
2003-12-24  8:53   ` Jean-Christophe Filliatre

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).