caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Yann Régis-Gianas" <yann@lrde.epita.fr> (by way of Yann Régis-Gianas <yann@lrde.epita.fr>)
To: caml-list@inria.fr
Subject: Re: [Caml-list] arbitrarily large integers
Date: Tue, 3 Dec 2002 10:47:09 +0100	[thread overview]
Message-ID: <200212031047.09974.yann@lrde.epita.fr> (raw)

Le Mardi 3 Décembre 2002 08:40, Scott J. a écrit :
> Hi,

	Hello,

> is it possible to implement in OCamel arbitrary large integers as
> in Dolphin smalltalk e.g. where e.g. factorial 10000 is evaluated
> very fast

	Yes, it's possible : just use the nums library (documented in the
documentation at http://caml.inria.fr/ocaml/htmlman/manual036.html).

	An example :

#load "nums.cma";;

(* directly with Big_int ... *)
open Big_int

let fact n =
  let rec fact_ acu p =
    if p = zero_big_int then
      acu
    else
      fact_ (mult_big_int acu p) (sub_big_int p unit_big_int)
  in
  fact_ unit_big_int n;;

string_of_big_int (fact (big_int_of_int 10000));;

(* more convenient with Num. *)

open Num

let n_ = num_of_int

let big_fact n =
  let rec fact_ acu p =
    if p = n_ 0 then
      acu
    else
      fact_ (acu */ p) (p -/ (n_ 1))
  in
  fact_ (n_ 1) n;;

string_of_num (big_fact (n_ 10000))


--
Yann Regis-Gianas
-------------------
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


             reply	other threads:[~2002-12-03 19:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-03  9:47 Yann Régis-Gianas [this message]
  -- strict thread matches above, loose matches on Subject: below --
2002-12-03  7:40 Scott J.
2002-12-03 13:00 ` Jean-Christophe Filliatre
2002-12-03 18:23 ` Alain.Frisch

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=200212031047.09974.yann@lrde.epita.fr \
    --to=yann@lrde.epita.fr \
    --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;
as well as URLs for NNTP newsgroup(s).