From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id TAA25596; Tue, 23 Dec 2003 19:58:56 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id TAA24257 for ; Tue, 23 Dec 2003 19:58:55 +0100 (MET) Received: from out2.smtp.messagingengine.com (out2.smtp.messagingengine.com [66.111.4.26]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id hBNIwsv05862 for ; Tue, 23 Dec 2003 19:58:54 +0100 (MET) X-Sasl-enc: wbnb1SCZyzIG0lINqSw66A 1072205892 Received: from [192.168.1.100] (user224.net229.nc.sprint-hsd.net [65.173.93.224]) by mail.messagingengine.com (Postfix) with ESMTP id 4E4C548A201 for ; Tue, 23 Dec 2003 13:58:12 -0500 (EST) Subject: [Caml-list] A (much less) frustrated beginner From: Tyler Eaves To: caml-list@inria.fr Content-Type: multipart/mixed; boundary="=-kOAH9kfI64qt6b7bJV3a" Message-Id: <1072206032.62516.27.camel@tylere> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.4.5 Date: Tue, 23 Dec 2003 14:00:38 -0500 X-Loop: caml-list@inria.fr X-Spam: no; 0.00; 950:99 primes:01 primes:01 isprime:01 printf:01 printf:01 isprime:01 int:01 int:01 rec:01 binary:02 float:02 float:02 optimized:02 exception:02 X-Attachments: name="primes2.ml" name="primes2.ml" Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk --=-kOAH9kfI64qt6b7bJV3a Content-Type: text/plain Content-Transfer-Encoding: 7bit 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! --=-kOAH9kfI64qt6b7bJV3a Content-Disposition: attachment; filename=primes2.ml Content-Type: text/plain; name=primes2.ml; charset= Content-Transfer-Encoding: 7bit (* primes2.ml Prime number generator, take 2 Tyler Eaves *) 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];; --=-kOAH9kfI64qt6b7bJV3a-- ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners