Re: combination function in python

2007-04-16 Thread [EMAIL PROTECTED]
On Apr 16, 6:40 pm, Anton Vredegoor <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > Isn't that what docstrings are for? Can't you leave > > the function name noverk() and add something to the > > effect of "this function calculates combinations"? > > Then it would show up in searches, wou

Re: combination function in python

2007-04-16 Thread Anton Vredegoor
[EMAIL PROTECTED] wrote: > Isn't that what docstrings are for? Can't you leave > the function name noverk() and add something to the > effect of "this function calculates combinations"? > Then it would show up in searches, wouldn't it? Yes, a doc string would help finding it in searches, however

Re: combination function in python

2007-04-15 Thread Alex Martelli
[EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > You could take it up with the gmpy author and > > induce him to get gmpy included in the standard distro if you are so > > inclined. > > Alex Martelli knows more about that subject than I and > it would be pointless for me to bug him about it. gmp

Re: combination function in python

2007-04-15 Thread [EMAIL PROTECTED]
On Apr 15, 4:16?pm, Anton Vredegoor <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > >> We're getting closer and closer to something I already posted a few > >> times here. This implementation was unfortunate because I consistently > >> used an uncommon name for it so people couldn't easily

Re: combination function in python

2007-04-15 Thread Steven D'Aprano
On Sun, 15 Apr 2007 13:58:38 -0700, Mark Dickinson wrote: > ... for large n and k it's difficult to imagine any real-world > applications that wouldn't be better served by using the lngamma > function instead. That is, the natural log of n choose k can be > computed much more quickly as > > lnga

Re: combination function in python

2007-04-15 Thread Anton Vredegoor
[EMAIL PROTECTED] wrote: >> We're getting closer and closer to something I already posted a few >> times here. This implementation was unfortunate because I consistently >> used an uncommon name for it so people couldn't easily find it > > But then, who's looking for it? The OP was trying to fin

Re: combination function in python

2007-04-15 Thread Mark Dickinson
On Apr 15, 3:33 pm, [EMAIL PROTECTED] wrote: > With few tests, it seems this is faster than the version by Jussi only > with quite big n,k. > True---and for large n and k it's difficult to imagine any real-world applications that wouldn't be better served by using the lngamma function instead. Th

Re: combination function in python

2007-04-15 Thread [EMAIL PROTECTED]
On Apr 15, 11:34�am, Anton Vredegoor <[EMAIL PROTECTED]> wrote: > Jussi Piitulainen wrote: > >> There's probably even a really clever way to avoid that final > >> division, but I suspect that would cost more in time and memory than > >> it would save. > > We're getting closer and closer to somethin

Re: combination function in python

2007-04-15 Thread bearophileHUGS
Mark Dickinson: > def choose(n, k): > ntok = 1 > for t in xrange(min(k, n-k)): > ntok = ntok*(n-t)//(t+1) > return ntok With few tests, it seems this is faster than the version by Jussi only with quite big n,k. (Another test to be added, is the assert n>0 of my original versio

Re: combination function in python

2007-04-15 Thread Jussi Piitulainen
Mark Dickinson writes: > Jussi Piitulainen wrote: > > > def choose(n, k): > >if 0 <= k <= n: > >ntok = 1 > >ktok = 1 > >for t in xrange(1, min(k, n - k) + 1): > > ntok *= n > > ktok *= t > > n -= 1 > >return ntok // ktok > >else:

Re: combination function in python

2007-04-15 Thread Anton Vredegoor
Jussi Piitulainen wrote: >> There's probably even a really clever way to avoid that final >> division, but I suspect that would cost more in time and memory than >> it would save. We're getting closer and closer to something I already posted a few times here. This implementation was unfortunate

Re: combination function in python

2007-04-15 Thread Mark Dickinson
On Apr 15, 8:37 am, Jussi Piitulainen <[EMAIL PROTECTED]> wrote: > def choose(n, k): >if 0 <= k <= n: >ntok = 1 >ktok = 1 >for t in xrange(1, min(k, n - k) + 1): > ntok *= n > ktok *= t > n -= 1 >return ntok // ktok >else: >

Re: combination function in python

2007-04-15 Thread Jussi Piitulainen
Steven D'Aprano writes: > bearophileHUGS wrote: ... >> return factorial(n) // (factorial(k) * factorial(n-k)) > > That's a naive and slow implementation. For even quite small values > of n and k, you end up generating some seriously big long ints, and > then have to (slowly!) divide them. A b

Re: combination function in python

2007-04-15 Thread bearophileHUGS
Steven D'Aprano: > That's a naive and slow implementation. For even quite small values > of n and k, you end up generating some seriously big long ints, > and then have to (slowly!) divide them. > A better implementation would be something like this: You are right, thank you for the improvement (t

Re: combination function in python

2007-04-15 Thread Steven D'Aprano
On Sun, 15 Apr 2007 02:38:31 -0700, bearophileHUGS wrote: > DanielJohnson: >> Please help, I couldnt find the function through help. > > You can't find it because it's not there: > > def factorial(n): > """factorial(n): return the factorial of the integer n. > factorial(0) = 1 > fact

Re: combination function in python

2007-04-15 Thread bearophileHUGS
DanielJohnson: > Please help, I couldnt find the function through help. You can't find it because it's not there: def factorial(n): """factorial(n): return the factorial of the integer n. factorial(0) = 1 factorial(n) with n<0 is -factorial(abs(n)) """ result = 1 for i in

Re: combination function in python

2007-04-14 Thread Alex Martelli
DanielJohnson <[EMAIL PROTECTED]> wrote: > how to use the combination function in python ? > > For example 9 choose 2 (written as 9C2) = 9!/7!*2!=36 > > Please help, I couldnt find the function through help. If you download and install gmpy, it's easy: >>>

combination function in python

2007-04-14 Thread DanielJohnson
how to use the combination function in python ? For example 9 choose 2 (written as 9C2) = 9!/7!*2!=36 Please help, I couldnt find the function through help. -- http://mail.python.org/mailman/listinfo/python-list