1. ## Feztaa

I saw your prime calculating program on CCAE. *Consider this one I made:

Code:
```#!/usr/bin/env python

import math, sys

def isPrime(n, sqroot):
*for x in xrange(2, sqroot+1):
* *if n % x == 0:
* * *return 0

*return 1

if len(sys.argv) &lt; 2:
*print &quot;Usage: %s &lt;Upper limit&gt;&quot; % sys.argv[0]
*sys.exit(1)

try:
*number = int(sys.argv[1])
except ValueError:
*print &quot;The limit must be an integer&quot;
*sys.exit(1)

sqroot = math.sqrt(number)

for i in xrange(1, number+1, 2):
*if isPrime(i, sqroot): print i```
It seems to be much faster (not to mention more proper)

2. ## Re: Feztaa

TMTOWTDI, guy.

My way demonstrates how to use an else clause in conjunction with a for loop (yes, that is a for...else statement in my script, no mistake).

3. ## Re: Feztaa

For every iteration you calculate the square root. Check the difference:

./prime.py 10000 0.34s user 0.01s system 75% cpu 0.466 total
./feztaa.py 10000 5.45s user 0.04s system 55% cpu 9.896 total
Kindda faster, heh?

4. ## Re: Feztaa

That's odd vince, seeing as mine doesn't take commandline arguments. It's interactive. Anyway, I'll look into it. It's not a priority right now.

5. ## Re: Feztaa

No need to look, I already said what was the problem: you calculate the square root way too often.

6. ## Re: Feztaa

I have to read it to change it don't I? Or did you just expect me to delete mine and replace it with yours? How am I going to learn anything if I don't edit my own code? :

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•