Prime number test

Announcements, comments, ideas, feedback, and "How do I... ?" questions
Post Reply
Gekkey
Posts: 1
Joined: Mon Jan 17, 2011 1:58 am

Prime number test

Post 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?
|_|0|_|
|_|_|0|
|0|0|0|
TripleM
Posts: 384
Joined: Fri Sep 12, 2008 3:31 am

Re: Prime number test

Post 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)?
Post Reply