Re: [Tutor] Finding even and odd numbers

2007-09-19 Thread Michael Langford
Premature optimization is the devil. Care not which method is faster. Only optimize when the whole program is too slow and then find which area is the biggest culprit and optimize that. Computers are very very fast these days. Even embedded systems are so fast you don't understand how fast they ar

Re: [Tutor] Finding even and odd numbers

2007-09-19 Thread Eric Brunson
>>> t = timeit.Timer( '1000%2') >>> t.timeit(100) 0.25893402099609375 >>> t = timeit.Timer( '1000&1') >>> t.timeit(100) 0.2567589282989502 It's almost insignificantly faster. However as a programmer and a mathematician, I disagree with your position that the bitwise operation is mo

Re: [Tutor] Finding even and odd numbers

2007-09-19 Thread Terry Carroll
On Wed, 19 Sep 2007, Boykie Mackay wrote: > if not n&1: > return false > > The above should return false for all even numbers,numbers being > represented by n.I have tried to wrap my head around the 'not n&1' but > I'm failing to understand what's going on.Could someone please explain > the s

Re: [Tutor] Finding even and odd numbers

2007-09-19 Thread taserian
> > > The above snippet is taking advantage of a similar property of binary > > numbers, which are base 2. What the above snippet is doing is checking > to > > see if that last digit is a 0 or not (asking "not n&1" is equivalent to > > asking "n&0", since that digit can only be a 0 or 1). > > Not q

Re: [Tutor] Finding even and odd numbers

2007-09-19 Thread Boykie Mackay
Thanks. The analogy to the decimal system was a big help in getting me to understand the principle. On Wed, 2007-09-19 at 09:47 -0400, taserian wrote: > On 9/19/07, Boykie Mackay <[EMAIL PROTECTED]> wrote: > Hi guys, > > I have come across a bit of code to find if a group

Re: [Tutor] Finding even and odd numbers

2007-09-19 Thread Ewald Ertl
Hi Boyks, Boykie Mackay wrote: > Hi guys, > > I have come across a bit of code to find if a group of numbers is odd or > even.The code snippet is shown below: > > if not n&1: > return false > > The above should return false for all even numbers,numbers being > represented by n.I have tried

Re: [Tutor] Finding even and odd numbers

2007-09-19 Thread Stephen Nelson-Smith
On 9/19/07, Boykie Mackay <[EMAIL PROTECTED]> wrote: > Hi guys, > > I have come across a bit of code to find if a group of numbers is odd or > even.The code snippet is shown below: > > if not n&1: > return false > > The above should return false for all even numbers,numbers being > represented

Re: [Tutor] Finding even and odd numbers

2007-09-19 Thread taserian
On 9/19/07, Boykie Mackay <[EMAIL PROTECTED]> wrote: > Hi guys, > > I have come across a bit of code to find if a group of numbers is odd or > even.The code snippet is shown below: > > if not n&1: >return false > > The above should return false for all even numbers,numbers being > represented

[Tutor] Finding even and odd numbers

2007-09-19 Thread Boykie Mackay
Hi guys, I have come across a bit of code to find if a group of numbers is odd or even.The code snippet is shown below: if not n&1: return false The above should return false for all even numbers,numbers being represented by n.I have tried to wrap my head around the 'not n&1' but I'm failing