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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:22:31+00:00 2026-05-29T15:22:31+00:00

The 10th problem in Project Euler: The sum of the primes below 10 is

  • 0

The 10th problem in Project Euler:

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

I found this snippet :

sieve = [True] * 2000000 # Sieve is faster for 2M primes
def mark(sieve, x):
    for i in xrange(x+x, len(sieve), x):
        sieve[i] = False

for x in xrange(2, int(len(sieve) ** 0.5) + 1):
    if sieve[x]: mark(sieve, x)

print sum(i for i in xrange(2, len(sieve)) if sieve[i]) 

published here
which run for 3 seconds.

I wrote this code:

def isprime(n):
    for x in xrange(3, int(n**0.5)+1):
        if n % x == 0:
            return False
    return True

sum=0;
for i in xrange(1,int(2e6),2):
    if isprime(i):
        sum += i

I don’t understand why my code (the second one) is much slower?

  • 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-29T15:22:32+00:00Added an answer on May 29, 2026 at 3:22 pm

    Your algorithm is checking every number individually from 2 to N (where N=2000000) for primality.

    Snippet-1 uses the sieve of Eratosthenes algorithm, discovered about 2200 years ago.
    It does not check every number but:

    • Makes a “sieve” of all numbers from 2 to 2000000.
    • Finds the first number (2), marks it as prime, then deletes all its multiples from the sieve.
    • Then finds the next undeleted number (3), marks it as prime and deletes all its multiples from the sieve.
    • Then finds the next undeleted number (5), marks it as prime and deletes all its multiples from the sieve.
    • …
    • Until it finds the prime 1409 and deletes all its multiples from the sieve.
    • Then all primes up to 1414 ~= sqrt(2000000) have been found and it stops
    • The numbers from 1415 up to 2000000 do not have to be checked. All of them who have not been deleted are primes, too.

    So the algorithm produces all primes up to N.

    Notice that it does not do any division, only additions (not even multiplications, and not that it matters with so small numbers but it might with bigger ones). Time complexity is O(n loglogn) while your algorithm has something near O(n^(3/2)) (or O(n^(3/2) / logn) as @Daniel Fischer commented), assuming divisions cost the same as multiplications.

    From the Wikipedia (linked above) article:

    Time complexity in the random access machine model is O(n log log n) operations, a direct consequence of the fact that the prime harmonic series asymptotically approaches log log n.

    (with n = 2e6 in this case)

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

Sidebar

Related Questions

I've made this snippet that clicks a link after 10th second: function timeout() {
Problem is like this: Suppose there are records in dataTable with FruitType as A
My query returns two days, say the 10th and the 11th, both just integers.
I've got an MS-Access app (1/10th MS-Acccess, 9/10ths MS-SQL) that needs to display photographs
I need to be able to schedule a job on the 10th of each
I'm currently making a SMS Application in Android, the following is a code snippet
I need a function that should give me a 10th or 100th array, for
I am getting the ApplicationError: 2 nonnumeric port: '' randomly for about 1/10th of
I have a specific problem with my calendar I'm making. Its difficult to explain
I have a vb.net MVC3 Razor app that generates PDF files. The problem is

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.