views:

133

answers:

2

I'm just getting back into Project Euler and have lost my account and solutions, so I'm back on problem 7. However, my code doesn't work. It seems fairly elementary to me, can someone help me debug my (short) script?

Should find the 10001st Prime.

#!/usr/bin/env python
#encoding: utf-8
"""
P7.py

Created by Andrew Levenson on 2010-06-29.
Copyright (c) 2010 __ME__. All rights reserved.
"""

import sys
import os
from math import sqrt

def isPrime(num):
    flag = True
    for x in range(2,int(sqrt(num))):
        if( num % x == 0 ):
            flag = False
    if flag == True:
         return True
    else:
         return False

def main():
    i, n = 1, 3
    p = False
    end = 6
    while end - i >= 0:
        p = isPrime(n)
        if p == True:
            i = i + 1
            print n
        n = n + 1

if __name__ == '__main__':
    main()

Edit*: Sorry, the issue is it says every number is prime. :/

+1  A: 

Isn't this better?

def isPrime(num):
    for x in range(2,int(sqrt(num))):
        if( num % x == 0 ):
            return False
    return True

And this:

def main():
    i, n = 1, 3
    while i <= 6:
        if isPrime(n):
            i = i + 1
            print n
        n = n + 1

Also, I'm not seeing a 10001 anywhere in there...

Eric
@Eric, your `isPrime` function has exactly the same bug as the OP's, see my answer.
Alex Martelli
Indeed. I was just pointing out the superfluous code. I figured that it would be rude to steal your fix.
Eric
Sorry, the '10001' was replaced by '6' for debugging purposes.
Andrew
+4  A: 

The syntax is fine (in Python 2). The semantics has some avoidable complications, and this off-by-one bug:

for x in range(2,int(sqrt(num))):
    if( num % x == 0 ):
        flag = False

range(2, Y) goes from 2 included to Y excluded -- so you're often not checking the last possible divisor and thereby deeming "primes" many numbers that aren't. As the simplest fix, try a 1 + int(... in that range. After which, removing those avoidable complications is advisable: for example,

if somebool: return True
else: return False

is never warranted, as the simpler return somebool does the same job.

A simplified version of your entire code (with just indispensable optimizations, but otherwise exactly the same algorithm) might be, for example:

from math import sqrt

def isPrime(num):
    for x in range(3, int(1 + sqrt(num)), 2):
        if num % x == 0: return False
    return True

def main():
    i, n = 0, 3
    end = 6
    while i < end:
        if isPrime(n):
            i += 1
            print n
        n += 2

if __name__ == '__main__':
    main()

"Return as soon as you know the answer" was already explained, I've added one more crucial optimization (+= 2, instead of 1, for n, as we "know" even numbers > 3 are not primes, and a tweak of the range for the same reason).

It's possible to get cuter, e.g.:

def isPrime(num):
    return all(num % x for x n range(3, int(1 + sqrt(num)), 2))

though this may not look "simpler" if you're unfamiliar with the all built-in, it really is, because it saves you having to do (and readers of the code having to follow) low level logic, in favor of an appropriate level of abstraction to express the function's key idea, that is, "num is prime iff all possible odd divisors have a [[non-0]] remainder when the division is tried" (i.e., express the concept directly in precise, executable form). The algorithm within is actually still identical.

Going further...:

import itertools as it

def odd():
    for n in it.count(1):
        yield n + n + 1

def main():
    end = 5        
    for i, n in enumerate(it.ifilter(isPrime, odd())):
        print n
        if i >= end: break

Again, this is just the same algorithm as before, just expressed at a more appropriate level of abstraction: the generation of the sequence of odd numbers (from 3 included upwards) placed into its own odd generator, and some use of the enumerate built-in and itertools functionality to avoid inappropriate (and unneeded) low-level expression / reasoning.

I repeat: no fundamental optimization applied yet -- just suitable abstraction. Optimization of unbounded successive primes generation in Python (e.g. via an open-ended Eratosthenes Sieve approach) has been discussed in depth elsewhere, e.g. here (be sure to check the comments too!). Here I was focusing on showing how (with built-ins such as enumerate, all, and any, the crucial itertools, plus generators and generator expressions) many "looping" problems can be expressed in modern Python at more appropriate levels of abstraction than the "C-inspired" ones that may appear most natural to most programmers reared on C programming and the like. (Perhaps surprisingly to scholars used to C++'s "abstraction penalty" first identified by Stepanov, Python usually tends to have an "abstraction premium" instead, especially if itertools, well known for its blazing speed, is used extensively and appropriately... but, that's really a different subject;-).

Alex Martelli
Not to mention the redundant checking after `Flag` is false, as in my answer
Eric
@Eric, sure, but that's extra work, not extra _complication_;-). `if somebool == True:` which the OP does often is just irrelevant extra complication, I was focusing on that right after correctness and before speed;-).
Alex Martelli
Thanks, I didn't realize Range specified an upper bound, not an upper limit.
Andrew
Also, @Alex, the if somebool == true was more of an "I can't figure out what's wrong, I'm so exasperated I'll try anything."
Andrew
@Andrew, I see -- would you like to see a "simple as it gets" version of the code?
Alex Martelli
@Alex, please! I'd love to see another way of doing it.
Andrew
@Andrew, there, I've shown and discussed a few -- enjoy!
Alex Martelli
Thanks much, @Alex! I wasn't aware of the third argument available for range() and I agree, the other optimizations are indispensable. I feel kind of foolish for leaving them out, as finding primes has been my goto problem for any new language.
Andrew
@Andrew, you're welcome! The optimizations are, in a sense, "incremental" (except for the "return ASAP" one, which @Eric pointed out) -- they don't change the big-O behavior, "just" the multiplier. (That one, I suspect but haven't proven, lowers the "average big-O" [[kind of an oxymoron hm?-)]] a bit, as [[handwaves]] "most" composite numbers have some "small" divisor).
Alex Martelli