-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solve 'Integers: Recreation One' kata
- Loading branch information
1 parent
504b0bd
commit 3cdff5a
Showing
3 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/main/kotlin/integersrecreationone/SumSquaredDivisors.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package solution | ||
|
||
import java.util.stream.LongStream | ||
import java.util.stream.LongStream.range | ||
|
||
/** | ||
* https://www.codewars.com/kata/55aa075506463dac6600010d/train/kotlin | ||
*/ | ||
object SumSquaredDivisors { | ||
|
||
fun listSquared(m: Long, n: Long): String { | ||
return (m..n) | ||
.asSequence() | ||
.map { Pair(it, it.divisors()) } | ||
.map { pair -> pair.mapSecond(LongStream::squareSum) } | ||
.filter { it.second.isSquare() } | ||
.map { it.toList() } | ||
.toList() | ||
.toString() | ||
} | ||
} | ||
|
||
fun Long.divisors(): LongStream { | ||
return range(1, this + 1) | ||
.takeWhile { it * it <= this } | ||
.filter { this % it == 0L } | ||
.flatMap { | ||
if (it * it == this) | ||
LongStream.of(it) | ||
else | ||
LongStream.of(it, this / it) | ||
} | ||
} | ||
|
||
fun LongStream.squareSum(): Long { | ||
return this.map { it * it }.sum() | ||
} | ||
|
||
fun Long.sqrt(): Long { | ||
return kotlin.math.sqrt(this.toDouble()).toLong() | ||
} | ||
|
||
fun Long.isSquare(): Boolean { | ||
val sqrt = this.sqrt() | ||
return sqrt * sqrt == this | ||
} | ||
|
||
fun Pair<Long, Long>.toList(): List<Long> { | ||
return listOf(this.first, this.second) | ||
} | ||
|
||
fun <A, B, C> Pair<A, B>.mapSecond(f: (B) -> C): Pair<A, C> { | ||
return Pair(this.first, f(this.second)) | ||
} |
13 changes: 13 additions & 0 deletions
13
src/test/kotlin/integersrecreationone/SumSquaredDivisorsTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package solution | ||
|
||
import org.junit.Test | ||
import kotlin.test.assertEquals | ||
|
||
class SumSquaredDivisorsTest { | ||
|
||
@Test | ||
fun basicTests() { | ||
assertEquals("[[1, 1], [42, 2500], [246, 84100]]", SumSquaredDivisors.listSquared(1, 250)) | ||
assertEquals("[[42, 2500], [246, 84100]]", SumSquaredDivisors.listSquared(42, 250)) | ||
} | ||
} |