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 6770607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:19:57+00:00 2026-05-26T15:19:57+00:00

This is my code in python for calculation of sum of prime numbers less

  • 0

This is my code in python for calculation of sum of prime numbers less than a given number.
What more can I do to optimize it?

import math
primes = [2,]                      #primes store the prime numbers



for i in xrange(3,20000,2):                    #i is the test number
    x = math.sqrt(i)
    isprime = True
    for j in primes:               #j is the devider. only primes are used as deviders
        if j <= x:
            if i%j == 0:
                    isprime = False
                    break


    if isprime:
        primes.append(i,)


print sum (primes,)
  • 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-26T15:19:57+00:00Added an answer on May 26, 2026 at 3:19 pm

    You can use a different algorithm called the Sieve of Eratosthenes which will be faster but take more memory. Keep an array of flags, signifying whether each number is a prime or not, and for each new prime set it to zero for all multiples of that prime.

    N = 10000
    
    # initialize an array of flags
    is_prime = [1 for num in xrange(N)]
    is_prime[0] = 0 # this is because indexing starts at zero
    is_prime[1] = 0 # one is not a prime, but don't mark all of its multiples!
    
    def set_prime(num):
        "num is a prime; set all of its multiples in is_prime to zero"
        for x in xrange(num*2, N, num):
            is_prime[x] = 0
    
    # iterate over all integers up to N and update the is_prime array accordingly
    for num in xrange(N):
        if is_prime[num] == 1:
            set_prime(num)
    
    primes = [num for num in xrange(N) if is_prime[num]]
    

    You can actually do this for pretty large N if you use an efficient bit array, such as in this example (scroll down on the page and you’ll find a Sieve of Eratosthenes example).

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

Sidebar

Related Questions

I was given this Python code that would calculate an MD5 value for any
/Users/smcho/Desktop/bracket/[10,20] directory has abc.txt, but when I run this Python code import glob import
I have this code in Python inputted = input(Enter in something: ) print(Input is
Consider this Python code for printing a list of comma separated values for element
I would like to do the equivalent off this (ruby code) in python for
I have a python script the runs this code: strpath = sudo svnadmin create
I tried this code to open a file in Python: f = open("/Desktop/temp/myfile.txt","file1") It
I'm porting some code from Python 2 to 3. This is valid code in
I am trying to understand this simple hashlib code in Python that has been
Is there a way to write this C/C++ code in Python? a = (b

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.