Re: Complexity question on Python 3 lists

2012-02-15 Thread Steven D'Aprano
On Wed, 15 Feb 2012 19:20:21 +0100, Franck Ditter wrote: > What is the cost of calling primes(n) below ? I'm mainly interested in > knowing if the call to append is O(1) Your primes() function appears to be a variation on trial division, which is asymptotically O(n*sqrt(n)/(log n)**2). Regardles

Re: Complexity question on Python 3 lists

2012-02-15 Thread Ian Kelly
On Wed, Feb 15, 2012 at 1:28 PM, Dennis Lee Bieber wrote: > On Wed, 15 Feb 2012 11:11:27 -0800, Chris Rebert > wrote: > > >>"The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, …" >>    -- list_resize() >> >        Rather perverse, is it not? The first set is plain doubling, but > then yo

Re: Complexity question on Python 3 lists

2012-02-15 Thread Terry Reedy
On 2/15/2012 2:11 PM, Chris Rebert wrote: It's slightly more complex: http://hg.python.org/cpython/file/096b31e0f8ea/Objects/listobject.c "The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, …" -- list_resize() This has apparently changed from time to time. -- Terry Jan Reedy -

Re: Complexity question on Python 3 lists

2012-02-15 Thread Chris Rebert
On Wed, Feb 15, 2012 at 10:43 AM, Ian Kelly wrote: > On Wed, Feb 15, 2012 at 11:20 AM, Franck Ditter wrote: >> Do lists in Python 3 behave like ArrayList in Java (if the capacity >> is full, then the array grows by more than 1 element) ? > > I believe the behavior in CPython is that if the array

Re: Complexity question on Python 3 lists

2012-02-15 Thread Stefan Behnel
Ian Kelly, 15.02.2012 19:43: > On Wed, Feb 15, 2012 at 11:20 AM, Franck Ditter wrote: >> Do lists in Python 3 behave like ArrayList in Java (if the capacity >> is full, then the array grows by more than 1 element) ? > > I believe the behavior in CPython is that if the array is full, the > capacity

Re: Complexity question on Python 3 lists

2012-02-15 Thread Ian Kelly
On Wed, Feb 15, 2012 at 11:20 AM, Franck Ditter wrote: > What is the cost of calling primes(n) below ? I'm mainly interested in > knowing if the call to append is O(1), even amortized. Yes, it's amortized O(1). See: http://wiki.python.org/moin/TimeComplexity >From a relatively shallow analysis

Re: Complexity question on Python 3 lists

2012-02-15 Thread Dave Angel
On 02/15/2012 01:20 PM, Franck Ditter wrote: What is the cost of calling primes(n) below ? I'm mainly interested in knowing if the call to append is O(1), even amortized. Do lists in Python 3 behave like ArrayList in Java (if the capacity is full, then the array grows by more than 1 element) ? d

Re: Complexity question on Python 3 lists

2012-02-15 Thread Chris Rebert
On Wed, Feb 15, 2012 at 10:20 AM, Franck Ditter wrote: > What is the cost of calling primes(n) below ? I'm mainly interested in > knowing if the call to append is O(1), even amortized. > Do lists in Python 3 behave like ArrayList in Java (if the capacity > is full, then the array grows by more tha

Complexity question on Python 3 lists

2012-02-15 Thread Franck Ditter
What is the cost of calling primes(n) below ? I'm mainly interested in knowing if the call to append is O(1), even amortized. Do lists in Python 3 behave like ArrayList in Java (if the capacity is full, then the array grows by more than 1 element) ? def sdiv(n) : # n >= 2 """returns the sm