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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:07:30+00:00 2026-05-23T13:07:30+00:00

Possible Duplicates: Fastest way to list all primes below N in python Checking if

  • 0

Possible Duplicates:
Fastest way to list all primes below N in python
Checking if a number is a prime number in Python

I am working on Project Euler Problem 10, which states as follows:

Find the sum of all the primes below two million.

Here’s my program:

numbers = []
sum = 0
range_number = 2000000

#Appends all numbers in range
for i in range(2, range_number):
    numbers.append(i)

#i is every entry in numbers, n is the multiples of numbers[i] starting at one 
#value of numbers[i] after i. This is the Sieve of Eratosthenes.
for i in range(0, len(numbers)-1):
    if numbers[i] != None:
        for n in range(i + numbers[i], len(numbers)-1, numbers[i]):
            numbers[n] = None

#Adds all the numbers that are not None
for i in numbers:
    if i != None:
        sum += i

print(sum)

My program changes all multiples of every number below the range to None, which should eliminate all composites and leave only primes.

When I plug in a simple number for range_number like 10, I get the wrong answer. Instead of just posting your own program, please tell me where I went wrong.
Other posts mentioned using the square root, but I didn’t really get that.

Thanks.

  • 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-23T13:07:30+00:00Added an answer on May 23, 2026 at 1:07 pm

    Your problem is that you never eliminate the last number in numbers. If range_number is 21, then len(numbers) is 20 and len(numbers)-1 is 19. So this line here:

    for n in range(i + numbers[i], len(numbers)-1, numbers[i]):
    

    Never actually removes the number 20 from the list. You could have seen this if you’d printed out the list. So currently your solution gives the correct answer if range_number is one more than a prime number, but is off by range_number-1 when range_number is one more than a composite.

    To fix that problem, simply change the line to be:

    for n in range(i + numbers[i], len(numbers), numbers[i]):
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Fastest way to list all primes below N in python Although I
Possible Duplicates: Merging dictionaries in C# What's the fastest way to copy the values
Possible Duplicates: Understanding “randomness” Fastest implementation of a true random number generator in C#
Possible Duplicate: Fastest way to determine if an integer's square root is an integer
Possible Duplicate: .NET - What’s the best way to implement a catch all exceptions
Possible Duplicate: How to check if a number is a power of 2 fastest
What is the fastest/shortest/one-liner (not possible :p) way to build a unique tree of
Possible Duplicates: Terminating a Python script Terminating a Python Program My question is how
Possible Duplicates: How to programmatically download image from website ? Download all images from
Possible Duplicates: Javascript - array.contains(obj) Best way to find an item in a JavaScript

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.