Project Euler Problem #1

From time to time I live to solve programming problems I find online. I find them to be a good brain exercise, and it keeps me sharp. One of the sites I like to use is Project Euler the problems found here are  mathematical in origin compared to other types you could do (but don’t worry I do plenty of those also).

So every so often expect a post like this one in which I dissect a Project Euler problem, and give my solution. It should be fun, and I hope you enjoy it as much as I do. Don’t worry my other type of content isn’t going to disappear.

Problem #1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 100

Note: I do understand there are more efficient solutions to the one’s I have written, and perhaps sometime in the foreseeable future I will go back, and write a more efficient solution.

This problem is simple enough we are looking for the sum of the numbers that are multiples of either 3 or 5 that are within 100.

Except I am going to go a little further let’s say we want to find the sum of all the numbers that are multiples of 3 or 5 less than N. This N could be 50, 100, or 2,000 it’s a place holder value in a sense. Those who are familiar with mathematical notation will recognize.

from sys import argv
n = argv[1]

This code is written to handle the command line argument so that we can run the program as follows.
python file.py [ARG]
Nothing fancy it’s just there to handle the input of the value of n.

So to solve this problem the approach is simple. We simply loop until n, and while doing so we test each value 0….N and if it’s divisible by 3 or 5 we add it to the sum. We check this by using modulus operation which returns the remainder of the two arguments. For example 42 % 2 = 0 because 42 is divisible by 2 without remainder. Thus we want to test if a number let’s call it x if x % 3 = 0 or x % 5 = 0 we know we are on the right track.

def problem1(n):
    """
    Find the sum of multiples of 3 and 5 less than n
    """
    n = int(n)
    answer = 0
    for n in range(0, n):
        if n % 3 == 0 or n % 5 == 0:
            answer += n
    return answer

print problem1(n)

If your confused by the or statement I used I did that to save time. You could have easily written two if statements testing each of the two conditions, but I am lazy, and wrote it all on one line saving me the effort. As you can see this is actually a pretty straightforward problem. Yet this isn’t the most efficient solution. If I ever get around to writing it myself you can expect another post on the matter.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s