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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:21:38+00:00 2026-05-27T19:21:38+00:00

First off, I apologize for my inaccurate vocabulary. I am an absolute ground-zero beginner.

  • 0

First off, I apologize for my inaccurate vocabulary. I am an absolute ground-zero beginner. Anyways, I am attempting to solve this problem:
http://projecteuler.net/problem=1

To be brief, I’m trying to write a script that will find the sum of all the multiples of 3 or 5 below 1000.

My (extremely basic) approach was with this program:

##Multiples of 3
x = range(3, 1000, 3)

##Multiples of 5
y = range(5, 1000, 5)

a = sum(x)
b = sum(y)
n = a + b

print n

I realized that this was wrong because there are numbers like 15 that are included twice (it’s a multiple of both 5 and 3).
So is there a way to fix this or am I approaching this problem from a completely wrong angle?
Or do I need to just study more before I try solving this problem?
I also apologize if this has been explained in a previous post, but I looked around for a bit.

  • 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-27T19:21:39+00:00Added an answer on May 27, 2026 at 7:21 pm

    It is called inclusion exclusion principle so you can do like

    ##Multiples of 3
    x = range(3, 1000, 3)
    
    ##Multiples of 5
    y = range(5, 1000, 5)
    
    ##multiple of 15 are counted twice
    z=range(15,1000,15)
    
    
    a = sum(x)
    b = sum(y)
    c = sum(z)
    n = a + b -c
    print(n)
    

    but beauty is in using generators or list comprehensions

    a = sum(i for i in range(1000) if i%3 == 0 or i%5 == 0 )
    print(a)
    

    Where % is modulo and is remainder in integer devision.
    Nice thing about this is that codes reads so fluently and is direct translation of rules and can be read from left to right.

    Both algorithms run times depends on n in this case is 1000. If n would be for instance 1000000000 you would have to wait for long time to complete. If you apply little mathematics you can find out that

    sum(a for a in range(a1,a2,n)) 
    

    is actually arithmetic progression and total of this can be calculated in constant time no matter how big n is. http://en.wikipedia.org/wiki/Project_Euler#Example_problem_and_solutions

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

Sidebar

Related Questions

First off, I apologize if this has been asked before. I can't seem to
First off, let me apologize if this has been asked already, but I can’t
First of all I apologize in advance for this question, a bit off the
First off I would like to apologize beforehand, in case this turns out to
First off, I'd like to apologize in advance for not knowing this. I've been
Moved: http://programmers.stackexchange.com/questions/21128/when-does-it-become-overkill Hi, First off, I apologize cause I don't know how to make
First off, I apologize if this question has already been asked. I searched but
First off, I apologize if this question has been asked before. I've done a
First off I apologize... I have posted this question before, but I did a
First off, I apologize if this is a confusing or backwards way to go

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.