Chris Angelico wrote:
> On Thu, Aug 4, 2011 at 4:01 AM, Steven D'Aprano
> wrote:
>> a, b = divmod(n, i)
>> if b == 0:
>> total += a+i
>>
>
> Wouldn't this fail on squares? It happens to give correct results as
> far as I've checked; no square up to 10,000 is called perfect, and
> there are no pe
On Thu, Aug 4, 2011 at 4:01 AM, Steven D'Aprano
wrote:
> a, b = divmod(n, i)
> if b == 0:
> total += a+i
>
Wouldn't this fail on squares? It happens to give correct results as
far as I've checked; no square up to 10,000 is called perfect, and
there are no perfect squares
harrismh777 wrote:
> def perf(n):
> sum = 0
> for i in range(1, n):
> if n % i == 0:
> sum += i
> return sum == n
A more effective way to speed that up is with a better algorithm:
def isperfect(n):
if n < 2: return False
total = 1
for i in range(
On Tue, Aug 2, 2011 at 3:45 PM, Steven D'Aprano
wrote:
> (But don't make the mistake of doing what I did, which was to attempt to
> produce range(29000) in Python 2. After multiple *hours* of swapping, I
> was finally able to kill the Python process and get control of my PC again.
> Sigh.)
>
harrismh777 wrote:
> The following is intended as a helpful small extension to the xrange()
> range() discussion brought up this past weekend by Billy Mays...
>
> With Python2 you basically have two ways to get a range of numbers:
> range() , which returns a list, and
>xrange() , which r
On Tue, Aug 2, 2011 at 10:05 AM, Peter Otten <__pete...@web.de> wrote:
> i/2 returns a float in Python 3; you should use i//2 for consistency.
>
And I forgot to make this change before doing my tests. Redoing the
Python 3 ones with // quite drastically changes things!
3.2 (r32:88445, Feb 20 2011,
On Tue, Aug 2, 2011 at 10:20 AM, Stefan Behnel wrote:
> What version of Py3 were you using? If you used the latest, maybe even the
> latest hg version, you will notice that that's substantially faster for
> integers than, e.g. 3.1.x.
>
I just tried this out, using a slightly modified script but t
Am 02.08.2011 09:12 schrieb harrismh777:
The following is intended as a helpful small extension to the xrange()
range() discussion brought up this past weekend by Billy Mays...
With Python2 you basically have two ways to get a range of numbers:
range() , which returns a list, and
xrange() , whic
harrismh777, 02.08.2011 09:12:
With Python2 you basically have two ways to get a range of numbers:
range() , which returns a list, and
xrange() , which returns an iterator.
With Python3 you must use range(), which produces an iterator; while
xrange() does not exist at all (at least not on 3.2).
harrismh777 wrote:
> The following is intended as a helpful small extension to the xrange()
> range() discussion brought up this past weekend by Billy Mays...
>
> With Python2 you basically have two ways to get a range of numbers:
> range() , which returns a list, and
>xrange() , which r
harrismh777 wrote:
these will run on either Python2 or
> Python3... except that if you substitute xrange() for range() for
> Python2 they will throw an exception on Python3... doh.
if 'xrange' not in dir(__builtins__):
xrange = range
at the beginning of your program will fix that.
--
The following is intended as a helpful small extension to the xrange()
range() discussion brought up this past weekend by Billy Mays...
With Python2 you basically have two ways to get a range of numbers:
range() , which returns a list, and
xrange() , which returns an iterator.
With Python
12 matches
Mail list logo