How to Solve Project Euler #1 in Kotlin

Today I’d like to introduce you to one of my favorite sources of programming problems: Project Euler.

Project Euler is composed of a large set of problems (779 as of this writing) that will challenge both your mathematical and programming skills. The motivation is to provide a fun and recreational learning platform for students and professionals interested in math and/or computer science.

What I like best about Project Euler’s problems is that they stretch each programming language in very different ways.

For the polyglot programmer, Project Euler provides great fodder for exploring the capabilities of multiple languages. Often one programming language’s features or libraries provide it with a competitive advantage, allowing it to solve certain Project Euler problems more efficiently or elegantly. This is especially true of languages with a strong functional bent.

Here’s our problem statement for today:

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.

Kotlin provides a powerful feature for attacking this problem: Lazy Sequences

What’s cool about a lazy sequence? It can be infinitely long! This allows us to describe infinite sequences like the set of all natural numbers in a single line of code, and we never encounter a stack overflow! We can do this because lazy sequences are not evaluated until we ask for the result of a multi-step process.

So in today’s solution, we take each factor and:

Multiply it by every number in our logically-infinite sequence of natural numbers. At this point, we haven’t evaluated anything.

Take all numbers from the sequence that are less than our upper bound of 1000. At this point, because we’ve established a concrete upper bound, Kotlin evaluates and returns a finite sequence of numbers.

Because we now have a sequence of sequences, we combine them into one using flatten.

We remove duplicates using distinct.

And finally, we take the sum.

Running our program tells us the answer: 233168.

To learn more about Kotlin Sequences, take a look at Sequences in the Kotlin documentation.

To see the code for this essay, take a look at its Atomic Repo at https://atomicrepos.dev/repos/project-euler-1-kotlin/.

Previous
Previous

How to Solve Project Euler #2 in Kotlin

Next
Next

How to Map/Filter/Reduce a List of Objects in Kotlin