Page 1 of 1

Prime number test

Posted: Mon Jan 17, 2011 2:11 am
by Gekkey
Alright. I am here to ask a question. I don't know if this belongs here or not.
I am using python and am on problem #10.
My program was going incredibly slow until I changed it from:

Code: Select all

for i in range(3, 2000000, 2):
        for i in range(2, x, 2):
to:

Code: Select all

for i in range(3, 2000000, 2):
        for i in range(2, x**0.5+1, 2):
I had seen that on other people's prime tests.
My question is: Why does this work?

Re: Prime number test

Posted: Mon Jan 17, 2011 2:38 am
by TripleM
Suppose j is a factor of i. Then so is i/j. Can both j and i/j be more than sqrt(i)?