Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77628

Re: My backwards logic

Newsgroups comp.lang.python
Date 2014-09-05 18:24 -0700
References (2 earlier) <CALwzid=ue9v62YdPRMqbqA4Bs5S71SHMsB+UUd4oB1a2J-xM6w@mail.gmail.com> <CAAp0bGsSfmD0Lnb2u4ADH0Qdj=xcaJhRmz61oBy5e-74V5hf1g@mail.gmail.com> <540A0582.1010403@mrabarnett.plus.com> <mailman.13807.1409946021.18130.python-list@python.org> <7x38c5u8z6.fsf@ruckus.brouhaha.com>
Message-ID <06b5d0e1-4e8c-44f7-bcd2-976b14004695@googlegroups.com> (permalink)
Subject Re: My backwards logic
From Rustom Mody <rustompmody@gmail.com>

Show all headers | View raw


On Saturday, September 6, 2014 1:37:57 AM UTC+5:30, Paul Rubin wrote:
> Juan Christian  writes:
> >  I made this code just for fun and learning, it's working, but would
> >  this be a good approach? Thanks. ...
> >  ** ** for number in range(start, stop + 1):
> >  ** ** ** ** divisors = [(number % x) for x in range(1, number + 1)]
> >  ** ** ** ** ** ** print("{n} prime? {r}".format(n = number, r =
> >  (divisors.count(0) == 2)))

> 1. Mathematically it's better to stop checking once you've gone past
> the square root of the number, since if n = p*q and is composite,
> then at least one of p,q will be <= sqrt(n).

> 2. As a program design matter, it's generally preferable for functions
> like this to not print the answer.  They should just return a value, in
> this case a bool indicating whether the number is prime.  That makes it
> easier to re-use the function, to wrap it in a unit test framework, etc.
> If you want to print the result, then call the function and have the
> caller print the returned value.

Hoo boy! And we come full circle

> 3. As a simple optimization, once you have checked that the number is
> not divisible by 2, you don't have to check for other even divisors.
> So just check for 2, then the odd numbers 3,5,7...

> This is a little bit fancy but I like to use itertools for these
> sorts of problems:

>   from itertools import chain, takewhile, count

>   def is_prime(n):
>       if n < 2: return False
>       candidates = chain([2], count(3,2))
>       return all(n%d!=0 for d in takewhile(lambda d: d*d<=n, candidates))

Paul's suggestions without the fancy -- no sqrt stop, no generators, no itertools
etc -- (all to be learnt at their time of course!)


First of all I make for myself a closed range

>>> def crange(a,b) : return range(a,b+1)

Check it

>>> crange(1,10)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Now for divisors of a number

>>> def divisors(n): return [fact for fact in crange(1,n) if n%fact == 0]

Check it

>>> divisors(4)
[1,2,4]

>>> divisors(7)
[1, 7]

So now a prime is a number n whose divisors are only 1 and n

>>> def is_prime(n): return divisors(n) == [1,n]

>>> is_prime(2)
True
>>> is_prime(3)
True
>>> is_prime(4)
False
>>> is_prime(5)
True
>>> is_prime(6)
False
>>> is_prime(7)
True

So collecting the code together

>>> def crange(a,b): return range(a,b+1)
>>> def divisors(n): return [fact for fact in crange(1,n) if n%fact == 0]
>>> def is_prime(n): return divisors(n) == [1,n]

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 12:48 -0400
  Re: My backwards logic MRAB <python@mrabarnett.plus.com> - 2014-09-05 17:57 +0100
  Re: My backwards logic Bob Gailer <bgailer@gmail.com> - 2014-09-05 13:04 -0400
  Re: My backwards logic John Gordon <gordon@panix.com> - 2014-09-05 17:08 +0000
  Re: My backwards logic Ethan Furman <ethan@stoneleaf.us> - 2014-09-05 10:08 -0700
    Re: My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 13:44 -0400
      Re: My backwards logic Chris Kaynor <ckaynor@zindagigames.com> - 2014-09-05 11:17 -0700
      Re: My backwards logic Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-05 15:04 -0600
      Re: My backwards logic Chris Angelico <rosuav@gmail.com> - 2014-09-06 09:44 +1000
  Re: My backwards logic Chris Kaynor <ckaynor@zindagigames.com> - 2014-09-05 10:09 -0700
  Re:My backwards logic Dave Angel <davea@davea.name> - 2014-09-05 13:15 -0400
  Re: My backwards logic Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-05 11:17 -0600
  Re: My backwards logic Juan Christian <juan0christian@gmail.com> - 2014-09-05 14:35 -0300
  Re: My backwards logic Ethan Furman <ethan@stoneleaf.us> - 2014-09-05 11:41 -0700
  Re: My backwards logic MRAB <python@mrabarnett.plus.com> - 2014-09-05 19:48 +0100
  Re: My backwards logic Juan Christian <juan0christian@gmail.com> - 2014-09-05 16:34 -0300
    Re: My backwards logic Paul Rubin <no.email@nospam.invalid> - 2014-09-05 13:07 -0700
      Re: My backwards logic Rustom Mody <rustompmody@gmail.com> - 2014-09-05 18:24 -0700
  Re: My backwards logic Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-05 20:54 +0100
  Re: My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 17:49 -0400
    Re: My backwards logic Chris Kaynor <ckaynor@zindagigames.com> - 2014-09-05 15:14 -0700
      Re: My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 18:36 -0400
    Re: My backwards logic Ian Kelly <ian.g.kelly@gmail.com> - 2014-09-05 16:35 -0600
      Re: My backwards logic Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-09-05 19:05 -0400
    Re: My backwards logic Dave Angel <davea@davea.name> - 2014-09-05 23:10 -0400
  Re: My backwards logic Juan Christian <juan0christian@gmail.com> - 2014-09-05 22:54 -0300
    Re: My backwards logic Rustom Mody <rustompmody@gmail.com> - 2014-09-05 18:58 -0700
  Posting style: interleaved responses (was: My backwards logic) Ben Finney <ben+python@benfinney.id.au> - 2014-09-06 12:37 +1000
  Re: Posting style: interleaved responses (was: My backwards logic) Juan Christian <juan0christian@gmail.com> - 2014-09-06 00:06 -0300
  Re: Posting style: interleaved responses (was: My backwards logic) Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-09-05 23:04 -0500
  Re: My backwards logic Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-06 08:45 +0100
  Re: My backwards logic Denis McMahon <denismfmcmahon@gmail.com> - 2014-09-06 07:49 +0000
    Prime testing [was Re: My backwards logic] Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-09-06 20:38 +1000
      Re: Prime testing [was Re: My backwards logic] Chris Angelico <rosuav@gmail.com> - 2014-09-06 20:42 +1000
      Re: Prime testing [was Re: My backwards logic] Manolo Martínez <manolo@austrohungaro.com> - 2014-09-06 12:53 +0200
        Re: Prime testing [was Re: My backwards logic] Peter Pearson <pkpearson@nowhere.invalid> - 2014-09-07 18:53 +0000
          Re: Prime testing [was Re: My backwards logic] Manolo Martínez <manolo@austrohungaro.com> - 2014-09-07 21:09 +0200
          Re: Prime testing [was Re: My backwards logic] Dan Stromberg <drsalists@gmail.com> - 2014-09-07 20:23 -0700
  Re: Posting style: interleaved responses Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-09-06 08:46 +0100

csiph-web