From tterribe@email.unc.edu Wed Mar 18 15:38:26 2009
Return-Path: <ccan-bounces+rusty=rustcorp.com.au@ozlabs.org>
X-Spam-Checker-Version: SpamAssassin 3.2.5 (2008-06-10) on ozlabs.org
X-Spam-Level: 
X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00 autolearn=ham
	version=3.2.5
X-Original-To: rusty@rustcorp.com.au
Delivered-To: rusty@ozlabs.org
Received: from ozlabs.org (localhost [127.0.0.1])
	by ozlabs.org (Postfix) with ESMTP id 5399ADDE0E
	for <rusty@rustcorp.com.au>; Thu, 19 Mar 2009 09:38:11 +1100 (EST)
X-Original-To: ccan@ozlabs.org
Delivered-To: ccan@ozlabs.org
X-Greylist: delayed 2513 seconds by postgrey-1.31 at ozlabs;
	Wed, 18 Mar 2009 16:50:38 EST
Received: from mxpm.isis.unc.edu (mxp3.isis.unc.edu [152.2.2.161])
	by ozlabs.org (Postfix) with ESMTP id 72C75DDEFA
	for <ccan@ozlabs.org>; Wed, 18 Mar 2009 16:50:37 +1100 (EST)
Received: from smtp.unc.edu (smtpsrv3.isis.unc.edu [152.2.2.251])
	by mxp3.isis.unc.edu (8.14.3/8.14.3) with ESMTP id n2I58gVL008719
	for <ccan@ozlabs.org>; Wed, 18 Mar 2009 01:08:42 -0400
Received: from [192.168.5.124] (pool-173-73-173-234.washdc.fios.verizon.net
	[173.73.173.234]) (authenticated bits=0)
	by smtp.unc.edu (8.14.3/8.14.3) with ESMTP id n2I58fex014904
	(version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT)
	for <ccan@ozlabs.org>; Wed, 18 Mar 2009 01:08:42 -0400 (EDT)
Message-ID: <49C081CA.5040501@email.unc.edu>
Date: Wed, 18 Mar 2009 01:08:26 -0400
From: "Timothy B. Terriberry" <tterribe@email.unc.edu>
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US;
	rv:1.8.1.11) Gecko/20071216 SeaMonkey/1.1.7
MIME-Version: 1.0
To: ccan@ozlabs.org
References: <c3cfaf5e0901190756x569da515s823fe4c7bd99c22a@mail.gmail.com>
	<200902040051.54761.rusty@rustcorp.com.au>
	<c3cfaf5e0902051926m6a2793e5pfdef62040becb4e7@mail.gmail.com>
	<200903181452.14263.rusty@rustcorp.com.au>
In-Reply-To: <200903181452.14263.rusty@rustcorp.com.au>
Content-Type: multipart/mixed;
  boundary="------------050107030903080507030408"
X-Proofpoint-Virus-Version: vendor=fsecure engine=1.12.7400:2.4.4, 1.2.40,
	4.0.166 definitions=2009-03-18_01:2009-03-13, 2009-03-18,
	2009-03-17 signatures=0
X-Proofpoint-Spam-Details: rule=uncdefault_notspam policy=uncdefault score=0
	spamscore=0 ipscore=0 phishscore=0 bulkscore=0 adultscore=0
	classifier=spam adjust=0 reason=mlx engine=5.0.0-0811170000
	definitions=main-0903170259
X-Mailman-Approved-At: Thu, 19 Mar 2009 09:38:09 +1100
Subject: Re: [ccan] Mathematics Module
X-BeenThere: ccan@ozlabs.org
X-Mailman-Version: 2.1.11
Precedence: list
List-Id: "If perl can, maybe ccan?" <ccan.ozlabs.org>
List-Unsubscribe: <https://ozlabs.org/mailman/options/ccan>,
	<mailto:ccan-request@ozlabs.org?subject=unsubscribe>
List-Archive: <http://ozlabs.org/pipermail/ccan>
List-Post: <mailto:ccan@ozlabs.org>
List-Help: <mailto:ccan-request@ozlabs.org?subject=help>
List-Subscribe: <https://ozlabs.org/mailman/listinfo/ccan>,
	<mailto:ccan-request@ozlabs.org?subject=subscribe>
Sender: ccan-bounces+rusty=rustcorp.com.au@ozlabs.org
Errors-To: ccan-bounces+rusty=rustcorp.com.au@ozlabs.org
Status: R
X-Status: NT
X-KMail-EncryptionState:  
X-KMail-SignatureState:  
X-KMail-MDN-Sent:  

This is a multi-part message in MIME format.
--------------050107030903080507030408
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

Some of this stuff seems a little trivial to include, but if we're going
to do so...

The result of nCr is guaranteed to be an integer after each iteration,
and so there's a) no reason to keep a separate numerator and denominator
and b) no reason to do a minimum of nine (9!) divisions every iteration.
Only a single gcd() is needed, and even that can often be skipped,
requiring only 2-3 divisions per iteration. This is illustrated in
choose.c (attached). You can easily add the check for the smaller r (_m
in this implementation), upgrade the types to long, etc., as desired. If
one really cared about speed, the divisions could be entirely replaced
by multiplications and a small lookup table, since m and n are known to
have a very limited range (or the result will overflow).

Also included are a gcd() implementation without tail recursion and
egcd(), the "extended" gcd, as well as a nice illustration of the use of
egcd, a Chinese Remainder Theorem solver for systems of linear
congruences. I don't have time to format these into a nice module with
test cases right now, but feel free to take what you want and throw the
rest in "junk". The code is (C) 1998-2001, except crt.c, which is (C)
1999-2001, 2007. I'm releasing it here under the LGPLv2 license.

--------------050107030903080507030408
Content-Type: application/x-bzip;
 name="nmbrthry.tar.bz2"
Content-Transfer-Encoding: base64
Content-Disposition: inline;
 filename="nmbrthry.tar.bz2"

QlpoOTFBWSZTWdZHi4IAD0t/nv/QAEB7//+/u2e8i//v/+4AQAAIAAGEgAAIYA798qVUUAAX
YNLYVbSAAAKUAAKAAAAcGQaNAGTTENNBkGIYQNAaMTEaAAAaCjTEBIHqAADT1D1AAGgAAAAA
HBkGjQBk0xDTQZBiGEDQGjExGgAAEmpFNRlNT/RGVN5U2mptT2qabUNqBiAyAADI8o09Eaeo
cGQaNAGTTENNBkGIYQNAaMTEaAAAKkgggjQEZAQ0Mk0jU8U9Gmo0eU9TxpQZkaMjU2p9kDMf
V9wivZ4gOTqn7wGbkUSRyyEA29t22fDfGfQl4fTBxbWtte1ra3VtnHjpqhvvQpVKhSqEVIKR
LJUUqJKoTo/kUfaDk4lVemwZOWUGotQXUVLMhTZuP2LZrGWV8VUvVp+8vi9qmK+7B9lHCkb8
yh+x2DE6DhzYZfIlH8CYGAmOI79GiqE3TyGgJLphSoomCpKsFYI2q9VcqgvgHEGoRkZPGHjJ
EPGFISTMkk5VIkWMIRGIiJBBA4Azk8QZLHw6jf1iIqr1V3v3GzlSqccvyMsyOsZOTBs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uUnwWTxs4fr1xj0ecHKf8OiS52vAtPaPbQ7ke6c3wYlk81PyK5+uST3
dQxP5w/pDrf0+iQ0DTzffT0Kemzzz540M+frmgo9tSSlQe06nkWvzaLSXzFUvwbLHfQnrko8
Io/YU5q/o6/979nHjdrOZbZWy8Onro7O0qFuos+UqWfQs7Iau1FvsHCk93tOx950SxD/+LuS
KcKEhrI8XBA=
--------------050107030903080507030408
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ccan mailing list
ccan@ozlabs.org
https://ozlabs.org/mailman/listinfo/ccan

--------------050107030903080507030408--
