Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 900415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:23:01+00:00 2026-05-15T15:23:01+00:00

I’m just getting back into Project Euler and have lost my account and solutions,

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T15:23:02+00:00Added an answer on May 15, 2026 at 3:23 pm

    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;-).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.