Ethiopian multiplication: Difference between revisions

(add task to aarch64 assembly raspberry pi)
Line 3,435:
 
=={{header|Kotlin}}==
 
<syntaxhighlight lang="scala">// version 1.1.2
 
Line 3,465 ⟶ 3,466:
99 x 99 = 9801
</pre>
 
=== Literally follow the described algorithm using generateSequence() ===
<syntaxhighlight lang="kotlin">
fun Int.halve() = this shr 1
fun Int.double() = this shl 1
fun Int.isOdd() = this and 1 == 1
 
 
fun ethiopianMultiply(n: Int, m: Int): Int =
generateSequence(Pair(n, m)) { p -> Pair(p.first.halve(), p.second.double()) }
.takeWhile { it.first >= 1 }.filter { it.first.isOdd() }.sumOf { it.second }
 
fun main() {
ethiopianMultiply(17, 34).also { println(it) } // 578
ethiopianMultiply(99, 99).also { println(it) } // 9801
ethiopianMultiply(4, 8).also { println(it) } // 32
}
</syntaxhighlight>
 
=={{header|Lambdatalk}}==
19

edits