Solutions for Project Euler Problems 1 to 3 in Kotlin

Malwinder Singh
3 min readMar 10, 2020
Tada!! It is Kotlin!

Hello friends... ☺

In this article, I shall tell you about a few puzzles in Kotlin along with the solutions. These questions came from the Project Euler website:

Before solving problems, we will create a Base class for our solution classes:

abstract class Solution {
abstract fun solve()
}

Also, here is our Main class:

class Main {
companion object {
@JvmStatic
fun main(a: Array<String>) {
//Replace `1` with Solution's class name
`1`().solve()
}
}
}

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

Here is the solution:

class `1` : Solution() {
override fun solve() {
//We first initialise variable sum
var sum = 0
//We run a loop from 1 to one less than 1000 (999)
for(i in 1..999) {
//If i is divisible by 3 or 5 then we add it to variable sum
if (i%3 == 0 || i%5== 0) {
sum+=i
}
}
//We print the sum variable
println(sum)
}
}

Answer: 233168

#2. Even Fibonacci numbers

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Here is the awesome solution:

class `2` : Solution() {
override fun solve() {
//We first create variables for Fibonacci series
var first = 0
var second = 1
//We will store result here
var sum = 0
//We create a loop
while (first + second < 4000000) {
val third = first + second
if (third % 2 == 0) {
//If the value of variable third is even then we add it to sum
sum += third
}
//We reassign first and second for next fibonacci number
first = second
second = third
}
println("Sum: $sum")
}
}

Answer: 233168

#3. Largest prime factor

The prime factors of 13195 are 5, 7, 13, and 29. What is the largest prime factor of the number 600851475143?

class `3` : Solution() {
companion object {
//We create compile time constant "NUM"
const val NUM = 600851475143
}

override fun solve() {
//Now, we need not to create loop for more than
// half of the number. Also, we start with 3
// since NUM is odd number and take step as 2 since
//it will never be divisible by 2.
for (i in 3..NUM / 2 step 2) {
//We skip for basic numbers 3, 5 and 7
if (i % 3 == 0L || i % 5 == 0L || i % 7 == 0L) {
continue
}
if (NUM % i == 0L) {
if (isPrime(i)) {
println("\n$i is prime and divides that no.")
}
}
}
}

/**
* If [num] is prime then return true. Return
* false otherwise.
*/
private fun isPrime(num: Long): Boolean {
for (i in 2..num / 2) {
if (num % i == 0L) {
return false
}
}
return true
}
}

Answer: 6857

Hope you liked this article!

Medium does not allow Indian writers to monetize currently 😭. Please support this blog by contributing here: https://www.patreon.com/malwinder 🥰

Buy me a coffee 🥰

Even a small contribution would help 😍

If you have any questions, then please write down a response in the response section.
If you liked this article, then please press the 👏 icon.
Thank you!
Peace! 🙂

--

--

Malwinder Singh

Flutter, Kotlin & Android. Worked on projects of Emerson and Omron, 50k+ views on this blog, 6k+ repo on StackOverflow linkedin.com/in/malwinder