pexels-photo.jpg

Project Euler Problem 1: Multiples of 3 and 5

Project Euler is a website that provides a series of challenging mathematical and computational problems designed to encourage and develop problem-solving skills in mathematics and computer programming. The problems on the website range in difficulty from relatively easy to extremely difficult, and cover a wide range of topics including number theory, combinatorics, and cryptography. The site provides a platform for programmers and mathematicians to share their solutions and discuss problem-solving techniques, and is a popular resource for those interested in improving their mathematical and computational skills. From time to time I choose one problem and solve it.

The first problem on Project Euler is a classic programming challenge that many developers encounter early on in their coding journey. The problem statement is as follows:

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 1000.

The problem seems simple enough – we need to find all the multiples of 3 or 5 below 1000, and then sum them up. But how do we approach this in code? Here’s one possible solution in C#:

public static int SumMultiplesOf3And5(int limit)
{
    int sum = 0;
    for (int i = 0; i < limit; i++)
    {
        if (i % 3 == 0 || i % 5 == 0)
        {
            sum += i;
        }
    }
    return sum;
}

In this solution, we use a loop to iterate over all the numbers from 0 to the specified limit. We then check if each number is a multiple of 3 or 5 using the modulo operator %. If the number is a multiple of either 3 or 5, we add it to a running sum.

To test our solution, let’s write some unit tests using NUnit:

[TestFixture]
public class Problem1Tests
{
    [Test]
    public void TestSumMultiplesOf3And5WithLimit10()
    {
        int result = Problem1.SumMultiplesOf3And5(10);
        Assert.AreEqual(23, result);
    }

    [Test]
    public void TestSumMultiplesOf3And5WithLimit1000()
    {
        int result = Problem1.SumMultiplesOf3And5(1000);
        Assert.AreEqual(233168, result);
    }
}

In these tests, we check the output of the SumMultiplesOf3And5 function for two different input values: 10 and 1000. The expected output for the first test is 23, which is the sum of the multiples of 3 and 5 below 10. The expected output for the second test is 233168, which is the sum of the multiples of 3 and 5 below 1000.

When we run these tests, they should both pass if our implementation is correct.

Overall, this problem is a great exercise in using loops and conditional statements to solve a simple mathematical problem. With a little bit of programming, we can easily find the sum of all the multiples of 3 or 5 below any given limit.

Mario Roecher

As an expert software engineer, manager, and leader, I am passionate about developing innovative solutions that drive business success. With an MBA and certifications as a software architect and Azure solution architect, I bring a unique blend of technical and business acumen to every project. I am deeply committed to integrating artificial intelligence into our solutions, always with a keen eye on ethical considerations to ensure responsible and fair AI deployment.

Beyond my professional pursuits, I am also an extreme sports enthusiast, with a love for windsurfing, mountain biking, and snowboarding. I enjoy traveling and experiencing new cultures and advocate for agile work models that prioritize flexibility, collaboration, and innovation. Let's connect and explore how we can drive transformative change together!