After testing the isprime function, this code works better for me.
def isprime(n):
'''check if integer n is a prime'''
# make sure n is a positive integer
n = abs(int(n))
# 0 and 1 are not primes
if n < 2:
return False
# 2 is the only even prime number
if n == 2:
return True
# all other even numbers are not primes
if not n & 1:
return False
# range starts with 3 and only needs to go up the squareroot of n
# for all odd numbers
for x in range(3, int(n**0.5)+1, 2):
if n % x == 0:
return False
return True
print isprime(17)
On Aug 16, 11:32 am, David <[email protected]> wrote:
> Thanks for the example.
>
> I purchased one book a couple of months ago. I don't recall which
> version though. I will have to go to Lulu and see if I can get
> another copy of the PDF from their site.
>
> On Aug 16, 11:13 am, NetAdmin <[email protected]> wrote:
>
> > I actually purchased a Hard-Copy edition of the Web2py Manual ( 2nd
> > edition )
>
> > The online book does NOT seem to have page numbers, but look at the
> > example below.
> > The Controller myconroller.py contains 1 function called isprime()
>
> > mycontroller.py
> > #
> > # mycontroller.py can contain multiple functions
> > #
> > def isprime( number ) :
> > #
> > # this is the isprime function
> > #
> > for p in range( 2, number ) :
> > if number % p :
> > return False
> > return True
>
> > Do you have a specific question about controllers?
>
> > On Aug 16, 10:35 am, David <[email protected]> wrote:
>
> > > Ok cool. Where can I find the 2nd Edition of the Web2Py book?
>
> > > I only see the 3rd edition online.
>
> > > On Aug 16, 10:22 am, NetAdmin <[email protected]> wrote:
>
> > > > Look at Chapter 3, Overview ( 2n edition of the manual )
>
> > > > On Aug 16, 8:50 am, David <[email protected]> wrote:
>
> > > > > Where is the part which talks about how controllers work and are used
> > > > > in the web2py book?
> > > > > I see the DAL and Views but nothing on controllers is obvious here.
>
> > > > > Thanks,
> > > > > David