Re: function that counts...

2010-06-11 Thread Bryan
Lie Ryan wrote: > In my original post in comp.programming, I > used this definition of factorial: > > def fact(n): >     """ factorial function (i.e. n! = n * (n-1) * ... * 2 * 1) """ >     p = 1 >     for i in range(1,n+1): >         p *= i >     return p Ah, much better, but partition10(M, i) ge

Re: function that counts...

2010-06-10 Thread Lie Ryan
On 06/10/10 09:03, Bryan wrote: > Lie Ryan wrote: >> I went through the mathematical foundation of using >> partition/distribution and inclusion-exclusion, and have written some >> code that solves a subset of the problem, feel free if you or superpollo >> are interested in continuing my answer (I

Re: function that counts...

2010-06-09 Thread Bryan
Lie Ryan wrote: > I went through the mathematical foundation of using > partition/distribution and inclusion-exclusion, and have written some > code that solves a subset of the problem, feel free if you or superpollo > are interested in continuing my answer (I won't be able to continue it > until n

Re: function that counts...

2010-06-09 Thread Albert van der Horst
In article , Albert van der Horst wrote: >In article <4bf442cd$0$31377$4fafb...@reader1.news.tin.it>, >superpollo wrote: >>... how many positive integers less than n have digits that sum up to m: >> >>In [197]: def prttn(m, n): >> tot = 0 >> for i in range(n): >> s = str(i) >>

Re: function that counts...

2010-05-28 Thread Lie Ryan
On 05/26/10 11:04, Bryan wrote: > Jean-Michel Pichavant wrote: >> I still don't see "how many positive integers less than n have digits >> that sum up to m" makes it a "partition" though if that what prttn >> means. Surely because I miss the context. > > A partition of a positive integer m is an u

Re: function that counts...

2010-05-26 Thread Albert van der Horst
In article <4bf442cd$0$31377$4fafb...@reader1.news.tin.it>, superpollo wrote: >... how many positive integers less than n have digits that sum up to m: > >In [197]: def prttn(m, n): > tot = 0 > for i in range(n): > s = str(i) > sum = 0 > for j in range(len(s)): >

Re: function that counts...

2010-05-26 Thread Bryan
I wrote: > > I came up with a recursive memo-izing algorithm that > > handles 100-digit n's. Oops. I missed Richard Thomas's post. He posted the same algorithm a couple days before. -- --Bryan -- http://mail.python.org/mailman/listinfo/python-list

Re: function that counts...

2010-05-26 Thread Bryan
I wrote: > My prttn() calls ndsums() once for each > digit, so the whole thing is polynomial in the number of digits. Correction: my prttn() function calls ndsums() at most 9 times per digit of n. That still provides run time polynomial in the length of the input. -- --Bryan -- http://mail.pyth

Re: function that counts...

2010-05-25 Thread Bryan
Jean-Michel Pichavant wrote: > I still don't see "how many positive integers less than n have digits > that sum up to m" makes it a "partition" though if that what prttn > means. Surely because I miss the context. A partition of a positive integer m is an unordered collection of positive integers

Re: function that counts...

2010-05-25 Thread Bryan
Raymond Hettinger wrote: > If speed is important, the global lookups can be localized: > > def prttn(m, n, map=itertools.imap, int=int, str=str, range=range): >     return sum(m == sum(map(int, str(x))) for x in range(n)) That's just silly. "If speed is important," we abandon the naive algorithm.

Re: function that counts...

2010-05-25 Thread superpollo
Jean-Michel Pichavant ha scritto: superpollo wrote: Jean-Michel Pichavant ha scritto: Jerry Hill wrote: On Wed, May 19, 2010 at 3:58 PM, superpollo wrote: ... how many positive integers less than n have digits that sum up to m: ... any suggestion for pythonizin' it? This is

Re: function that counts...

2010-05-25 Thread Jean-Michel Pichavant
superpollo wrote: Jean-Michel Pichavant ha scritto: Jerry Hill wrote: On Wed, May 19, 2010 at 3:58 PM, superpollo wrote: ... how many positive integers less than n have digits that sum up to m: ... any suggestion for pythonizin' it? This is how I would do it: def prttn(m, n

Re: function that counts...

2010-05-24 Thread superpollo
Jean-Michel Pichavant ha scritto: Jerry Hill wrote: On Wed, May 19, 2010 at 3:58 PM, superpollo wrote: ... how many positive integers less than n have digits that sum up to m: ... any suggestion for pythonizin' it? This is how I would do it: def prttn(m, n): """How many

Re: function that counts...

2010-05-24 Thread Matteo Landi
What about avoiding the string conversion and use mod/div operations in order to create a list of digits for a number? Now that you have the sequences it's easy to count which sums up to m. On Mon, May 24, 2010 at 4:14 PM, Raymond Hettinger wrote: > On May 19, 12:58 pm, superpollo wrote: >> ...

Re: function that counts...

2010-05-24 Thread Jean-Michel Pichavant
Jerry Hill wrote: On Wed, May 19, 2010 at 3:58 PM, superpollo wrote: ... how many positive integers less than n have digits that sum up to m: ... any suggestion for pythonizin' it? This is how I would do it: def prttn(m, n): """How many positive integers less than n hav

Re: function that counts...

2010-05-24 Thread Raymond Hettinger
On May 19, 12:58 pm, superpollo wrote: > ... how many positive integers less than n have digits that sum up to m: > > In [197]: def prttn(m, n): >      tot = 0 >      for i in range(n): >          s = str(i) >          sum = 0 >          for j in range(len(s)): >              sum += int(s[j]) >  

Re: function that counts...

2010-05-22 Thread Bryan
I wrote: > I came up with a recursive memo-izing algorithm that > handles 100-digit n's. [...] I made a couple improvements. Code below. -Bryan #- _nds = {} def ndsums(m, d): """ Count d-digit ints with digits suming to m. """ assert m >= 0 and d >= 0 m = min

Re: function that counts...

2010-05-21 Thread Bryan
Peter Pearson wrote: > If it's important for the function to execute quickly for large n, > you might get a useful speedup by testing only every ninth integer, Our standards for "quickly" and "large" seem kind of thin. > I suspect that further applications of number theory would > provide additio

Re: function that counts...

2010-05-21 Thread Albert van der Horst
In article , Grant Edwards wrote: > >Lest my allusions to Fortran IV be lost upon the less grizzled, only >the first 6 characters were significant in Fortran IV identifiers, and >removing all of the vowels from a longer word was an idiomatic way to >create an identifier with a length <= 6. > >IIR

Re: function that counts...

2010-05-20 Thread D'Arcy J.M. Cain
On Thu, 20 May 2010 19:53:42 + (UTC) Grant Edwards wrote: > Since Python is interactive, and you don't get charged for each time > you run your deck through the reader, that's easy enough to check: Whoa! For a moment there I thought it was 1969. :-) -- D'Arcy J.M. Cain | Democra

Re: function that counts...

2010-05-20 Thread Grant Edwards
On 2010-05-20, superpollo wrote: > Grant Edwards ha scritto: >> On 2010-05-20, superpollo wrote: >>> Steven D'Aprano ha scritto: On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote: > ... how many positive integers less than n have digits that sum up to m: Does the name "

Re: function that counts...

2010-05-20 Thread superpollo
Grant Edwards ha scritto: On 2010-05-20, superpollo wrote: Steven D'Aprano ha scritto: On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote: ... how many positive integers less than n have digits that sum up to m: In [197]: def prttn(m, n): Does the name "prttn" mean anything? I'm afraid I

Re: function that counts...

2010-05-20 Thread Grant Edwards
On 2010-05-20, superpollo wrote: > Steven D'Aprano ha scritto: >> On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote: >> >>> ... how many positive integers less than n have digits that sum up to m: >>> >>> In [197]: def prttn(m, n): >> >> Does the name "prttn" mean anything? I'm afraid I keep

Re: function that counts...

2010-05-20 Thread superpollo
Peter Pearson ha scritto: On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote: ... how many positive integers less than n have digits that sum up to m: If it's important for the function to execute quickly for large n, you might get a useful speedup by testing only every ninth integer, since

Re: function that counts...

2010-05-20 Thread Peter Pearson
On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote: > ... how many positive integers less than n have digits that sum up to m: If it's important for the function to execute quickly for large n, you might get a useful speedup by testing only every ninth integer, since any two integers whose digi

Re: function that counts...

2010-05-20 Thread superpollo
Richard Thomas ha scritto: For this kind of problem you should avoid all that stringification. I find it best to deal with sequences of digits of a fixed length and go from there. For example: def count1(m, n, cache={}): """Number of digit sequences of length `n` summing to `m`.""" if n

Re: function that counts...

2010-05-20 Thread superpollo
Steven D'Aprano ha scritto: On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote: ... how many positive integers less than n have digits that sum up to m: In [197]: def prttn(m, n): Does the name "prttn" mean anything? I'm afraid I keep reading it as a mispelling of "print n". pArtItIOn

Re: function that counts...

2010-05-20 Thread superpollo
Steven D'Aprano ha scritto: On Wed, 19 May 2010 22:58:22 +0200, superpollo wrote: In [266]: del(sum) del is a statement, not a function, so the brackets are pointless. This is like writing: x = (1) instead of x = 1 `del sum` is all you need. sorry about that, thanks a lot! bye -- ht

Re: function that counts...

2010-05-19 Thread John Posner
On 5/19/2010 5:51 PM, Steven D'Aprano wrote: On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote: Rather than iterating over an index j = 0, 1, 2, ... and then fetching the jth character of the string, you can iterate over the characters directly. So the inner loop is better written: for c in

Re: function that counts...

2010-05-19 Thread Richard Thomas
For this kind of problem you should avoid all that stringification. I find it best to deal with sequences of digits of a fixed length and go from there. For example: def count1(m, n, cache={}): """Number of digit sequences of length `n` summing to `m`.""" if n < 0 or m < 0: return

Re: function that counts...

2010-05-19 Thread Steven D'Aprano
On Wed, 19 May 2010 21:58:04 +0200, superpollo wrote: > ... how many positive integers less than n have digits that sum up to m: > > In [197]: def prttn(m, n): Does the name "prttn" mean anything? I'm afraid I keep reading it as a mispelling of "print n". [...] > s = str(i) >

Re: function that counts...

2010-05-19 Thread Steven D'Aprano
On Wed, 19 May 2010 22:58:22 +0200, superpollo wrote: > In [266]: del(sum) del is a statement, not a function, so the brackets are pointless. This is like writing: x = (1) instead of x = 1 `del sum` is all you need. -- Steven -- http://mail.python.org/mailman/listinfo/python-list

Re: function that counts...

2010-05-19 Thread René 'Necoro' Neumann
Am 19.05.2010 22:58, schrieb superpollo: > > In [277]: prttn(25, 1) > Out[277]: 348 > > In [278]: prttn2(25, 1) > Out[278]: 348 > > In [279]: prttn3(25, 1) > Out[279]: 348 > > ok, bye! Just because I was curios: nec...@zakarumiy ~ % python -m timeit "import test; test.prttn(25,100

Re: function that counts...

2010-05-19 Thread superpollo
Mark Dickinson ha scritto: On May 19, 9:30 pm, superpollo wrote: René 'Necoro' Neumann ha scritto: An idea would be: def prttn(m, n): ...return sum(1 for x in range(n) if sum(map(int, str(x))) == m) TypeError: 'int' object is not callable on 2.5.4 The TypeError is almost certainl

Re: function that counts...

2010-05-19 Thread superpollo
Jerry Hill ha scritto: On Wed, May 19, 2010 at 4:25 PM, superpollo wrote: Jerry Hill ha scritto: sumofdigits = sum(int(char) for char in str(testval)) this line gives me this: TypeError: 'int' object is not callable is it some new feature in >2.5 ? No, sum() has been a builtin sinc

Re: function that counts...

2010-05-19 Thread Mark Dickinson
On May 19, 9:30 pm, superpollo wrote: > René 'Necoro' Neumann ha scritto: > > An idea would be: > > def prttn(m, n): > > ...        return sum(1 for x in range(n) if sum(map(int, str(x))) == m) > > TypeError: 'int' object is not callable > > on 2.5.4 The TypeError is almost certainly because

Re: function that counts...

2010-05-19 Thread Jerry Hill
On Wed, May 19, 2010 at 4:25 PM, superpollo wrote: > Jerry Hill ha scritto: >>        sumofdigits = sum(int(char) for char in str(testval)) > > this line gives me this: > > TypeError: 'int' object is not callable > > is it some new feature in >2.5 ? No, sum() has been a builtin since Python 2.3.

Re: function that counts...

2010-05-19 Thread superpollo
René 'Necoro' Neumann ha scritto: Am 19.05.2010 21:58, schrieb superpollo: ... how many positive integers less than n have digits that sum up to m: In [197]: def prttn(m, n): tot = 0 for i in range(n): s = str(i) sum = 0 for j in range(len(s)): sum +=

Re: function that counts...

2010-05-19 Thread superpollo
Jerry Hill ha scritto: On Wed, May 19, 2010 at 3:58 PM, superpollo wrote: ... how many positive integers less than n have digits that sum up to m: ... any suggestion for pythonizin' it? This is how I would do it: def prttn(m, n): """How many positive integers less than n have digits th

Re: function that counts...

2010-05-19 Thread René 'Necoro' Neumann
Am 19.05.2010 21:58, schrieb superpollo: > ... how many positive integers less than n have digits that sum up to m: > > In [197]: def prttn(m, n): > tot = 0 > for i in range(n): > s = str(i) > sum = 0 > for j in range(len(s)): > sum += int(s[j]) >

Re: function that counts...

2010-05-19 Thread Jerry Hill
On Wed, May 19, 2010 at 3:58 PM, superpollo wrote: > ... how many positive integers less than n have digits that sum up to m: ... > any suggestion for pythonizin' it? This is how I would do it: def prttn(m, n): """How many positive integers less than n have digits that sum up to m""" tot

function that counts...

2010-05-19 Thread superpollo
... how many positive integers less than n have digits that sum up to m: In [197]: def prttn(m, n): tot = 0 for i in range(n): s = str(i) sum = 0 for j in range(len(s)): sum += int(s[j]) if sum == m: tot += 1 return tot .: