Keen eyes

Chris Angelico rosuav at gmail.com
Sat Jan 16 17:30:26 EST 2016


On Sun, Jan 17, 2016 at 9:23 AM,  <jonas.thornvall at 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



More information about the Python-list mailing list