A solution for the Project Euler problem number 12. Written in the Java-like language Kotlin. Performance optimizations have primarily been made in the function for finding divisors.

Project Euler problem number 12 - Highly Divisible Triangular Number - is stated as follows.

The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The number 28 has six divisors - 1, 2, 4, 7, 14, 28.

What is the value of the first triangle number to have over five hundred divisors?

```
fun main(args: Array<String>) {
// Set the target to 500 divisors.
val target = 500
// Create a sequence that steps 1 at the time.
val triangleNumber = generateSequence(1, { it + 1 })
// Get the triangle number for every number.
.map { getTriangleNumber(it) }
// Stop when we find a triangle number with more divisors
// than our target.
.first { getDivisors(it).size > target }
// Print the answer.
println("$triangleNumber is the first triangle number with more than $target divisors.")
}
fun getTriangleNumber(n: Int): Int {
// Triangle numbers are given by the following formula.
// n(n+1)/2
return (n*(n+1)/2.0).toInt()
}
fun getDivisors(n: Int): List<Int> {
// Factors comes in pairs, you only need to iterate to the
// square root of n and get the paired factor using n / it.
val limit = kotlin.math.sqrt(n.toDouble()).toInt()
return (1..limit).filter { n % it == 0 }.flatMap {
val squaredIsN = it * it == n
if (squaredIsN) listOf(it) else listOf(it, n / it)
}
}
```

You can run and modify the code at https://try.kotlinlang.org.

- [project-euler] Project Euler Problem 17 - Number Letter Counts - solved with R
- [project-euler] Project Euler Problem 16 - Power Digit Sum - solved with Javascript
- [project-euler] Project Euler Problem 15 - Lattice Paths - solved with R
- [project-euler] Finding the 10001st prime with R
- [project-euler] Find longest Collatz sequence using Javascript
- [project-euler] Sum large numbers with Kotlin versus Javascript
- [tech-test] What is a binary tree and how to invert it using Kotlin
- [project-euler] Find Highly Divisible Triangular Numbers with Kotlin
- [project-euler] Find the Largest Product in a Grid with Rust
- [project-euler] Summation of the First Two Million Primes with Rust
- [project-euler] Finding the largest product in a series with Rust
- [project-euler] Finding the 10001st prime with Rust
- [js] The complete list of rational numbers with Stern-Brocot and Javascript
- [project-euler] Project Euler number six solved with Rust
- [project-euler] Smallest positive number that is evenly divisible by all of the numbers from 1 to 20 with Rust
- [project-euler] Finding the largest palindrome product with Rust
- [project-euler] Get the largest prime factor with Reason
- [project-euler] Find the sum of all even Fibonacci numbers below four million with OCaml
- [project-euler] Finding the sum of all multiples of 3 or 5 below 1000 with OCaml
- [project-euler] Special Pythagorean Triplet solved with Reason
- [personal] New Year's Resolution 2018