DEV Community

loading...

Project Euler | #1 - Multiples of 3 and 5

renecvt profile image René Vidríales Trujillo ・2 min read

Hello! This is the start of a personal new years resolution which I hope will help me grow as a developer and will assist me with my writing and problem solving skills. Let's begin.

First and foremost, let's define some things before diving into the actual solution I came up with.

What is Project Euler?

From their site:

Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

OK so, this sounds easy. Project Euler is composed of 687 problems and each problem requires some mathematical knowledge and programming skills. Any programming language can be used. For today's problem I will be using Haskell.

What's Haskell?

From the Haskell Wiki:

Haskell is a modern, standard, non-strict, purely-functional programming language. It provides all the features sketched above, including polymorphic typing, lazy evaluation and higher-order functions. It also has an innovative type system which supports a systematic form of overloading and a module system.

I'd love to write more about this language, but I think that it deserves a whole post. In my opinion I believe this definition is enough for you to get an idea of what Haskell is.

Why Haskell?

I chose this language because I have used it before at school and I loved it!

It was challenging for me because my programming background was very object oriented so I didn't know almost anything about the functional paradigm. I like how Haskell manages lists, flows which depend on conditionals, it's lazy evaluation, among other great things.

Let's dive into the problem.

Problem definition

Problem 1 | Multiples of 3 and 5

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.

Think about how can you solve it with your favourite language. Or you could try to lay it down first with a flowchart. Options are endless.

As for me, I tend to write things down on paper when they are complicated or have many constraints. After laying it down on paper I then implement a solution based on the criteria and constraints I got.

Following this approach I get bugs most of the times, but I believe this bugs are good because often I find validations or new constraints that I hadn't planned for when I was first sketching it down.

Solution

Without much further ado, this is the solution I came up with.

sum [x | x <- [1..999], x `mod` 3 == 0 || x `mod` 5 == 0]

This can be expressed as:

Fill a list with numbers ranging from 1 to 999 which are divisible by three or five. Then, compute the sum of all the values in the list.

And that's it! I won't spoil you with the answer. Solve it yourself and, have fun!

Thank you for reading.

Discussion (0)

pic
Editor guide