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. :/
The syntax is fine (in Python 2). The semantics has some avoidable complications, and this off-by-one bug:
range(2, Y)goes from 2 included toYexcluded — so you’re often not checking the last possible divisor and thereby deeming “primes” many numbers that aren’t. As the simplest fix, try a1 + int(...in thatrange. After which, removing those avoidable complications is advisable: for example,is never warranted, as the simpler
return somebooldoes the same job.A simplified version of your entire code (with just indispensable optimizations, but otherwise exactly the same algorithm) might be, for example:
“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 therangefor the same reason).It’s possible to get cuter, e.g.:
though this may not look “simpler” if you’re unfamiliar with the
allbuilt-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…:
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
oddgenerator, and some use of theenumeratebuilt-in anditertoolsfunctionality 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, andany, the crucialitertools, 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 ifitertools, well known for its blazing speed, is used extensively and appropriately… but, that’s really a different subject;-).