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


Groups > comp.lang.python > #101821

Re: Keen eyes

From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Subject Re: Keen eyes
Date 2016-01-17 09:30 +1100
Message-ID <mailman.47.1452983435.15297.python-list@python.org> (permalink)
References <d8e5eaf4-c59f-4635-8e73-f4a9a1a84877@googlegroups.com>

Show all headers | View raw


On Sun, Jan 17, 2016 at 9:23 AM,  <jonas.thornvall@gmail.com> wrote:
> function factor_it(i){
> prime=true;
> sqroot=Math.floor(Math.sqrt(i));
> for (j=2;j<sqroot;j++){ k=i/j; prime=!Number.isInteger(k); if (prime) {return prime}}
> return prime;
> }

A couple of potential problems here. The first thing that comes to
mind is that floating point inaccuracy is going to bite you long
before the numbers "seem huge" to someone who's thinking about 2**53.
The second is an off-by-one error: a perfect square may come up as
prime.

Check for those and see how it looks.

Also, check your double-use of the 'prime' variable, which also
appears to be global here.

ChrisA

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


Thread

Keen eyes jonas.thornvall@gmail.com - 2016-01-16 14:23 -0800
  Re: Keen eyes Chris Angelico <rosuav@gmail.com> - 2016-01-17 09:30 +1100
    Re: Keen eyes jonas.thornvall@gmail.com - 2016-01-16 14:55 -0800
    Re: Keen eyes jonas.thornvall@gmail.com - 2016-01-16 15:25 -0800
      Re: Keen eyes Steven D'Aprano <steve@pearwood.info> - 2016-01-17 18:10 +1100
        Re: Keen eyes Ian Kelly <ian.g.kelly@gmail.com> - 2016-01-17 00:37 -0700
        Re: Keen eyes Paul Rubin <no.email@nospam.invalid> - 2016-01-16 23:52 -0800
        Re: Keen eyes Chris Angelico <rosuav@gmail.com> - 2016-01-17 19:25 +1100
          Re: Keen eyes BartC <bc@freeuk.com> - 2016-01-17 11:17 +0000
            Re: Keen eyes Chris Angelico <rosuav@gmail.com> - 2016-01-17 22:32 +1100

csiph-web