From: Tyler Eaves <tyler@ml1.net>
To: caml-list@inria.fr
Subject: [Caml-list] A (much less) frustrated beginner
Date: Tue, 23 Dec 2003 14:00:38 -0500 [thread overview]
Message-ID: <1072206032.62516.27.camel@tylere> (raw)
[-- 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];;
next reply other threads:[~2003-12-23 18:58 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-12-23 19:00 Tyler Eaves [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1072206032.62516.27.camel@tylere \
--to=tyler@ml1.net \
--cc=caml-list@inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox