File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change 1+ fun main (args : Array <String >) {
2+ print (karatsuba(923 , 1723 ))
3+ }
4+ fun karatsuba (x : Int , y : Int ):Int {
5+ if ((x).toString().length == 1 || ((y).toString().length == 1 ))
6+ {
7+ return x * y
8+ }
9+ else
10+ {
11+ val n = Math .max((x).toString().length, (y).toString().length)
12+ val z = n / 2
13+ val a = x / Math .pow(10.0 , z.toDouble()).toInt()
14+ val b = x % Math .pow(10.0 , z.toDouble()).toInt()
15+ val c = y / Math .pow(10.0 , z.toDouble()).toInt()
16+ val d = y % Math .pow(10.0 , z.toDouble()).toInt()
17+ val ac = karatsuba(a, c)
18+ val bd = karatsuba(b, d)
19+ val adbc = karatsuba(a + b, c + d) - ac - bd
20+ return ac * Math .pow(10.0 , (2 * z).toDouble()).toInt() + (adbc * Math .pow(10.0 , z.toDouble()).toInt()) + bd
21+ }
22+ }
You can’t perform that action at this time.
0 commit comments