Theory
From verse to Kotlin
The last two topics covered the Lilavati's documented mathematics: a multiplication method that breaks numbers into parts, and the right-triangle relationship. Your syllabus asks you to do something satisfying with them: implement them in code.
Since this subject is Kotlin, you can implement them in the very language you just learned, applying fun, val, and if-expressions to nearly-900-year-old mathematics. This is a normal programming exercise that connects a classical text to your fresh Kotlin skills. This lesson shows both methods as short Kotlin programs, with their outputs, so you can see the Lilavati's arithmetic run on a modern machine.
Theory
Implementing the multiplication method
The Lilavati's part-by-part multiplication (from topic 1) breaks the multiplier into place-value parts and sums the partial products, which is the distributive law. In Kotlin, the simplest faithful version processes the multiplier digit by digit (by place value), multiplies, and adds.
For the worked example 125 × 12, splitting 12 into 10 + 2 gives 125×10 + 125×2 = 1250 + 250 = 1500. (Of course Kotlin's * multiplies directly; the point of the exercise is to EXPRESS the documented METHOD, showing how the historical technique maps to code, not to replace the operator.)
Practical
Part-by-part multiplication (the documented method)
// Multiply by breaking the multiplier into place-value parts,
// summing partial products (the Lilavati's distributive method).
fun multiplyByParts(a: Int, b: Int): Int {
var total = 0
var place = 1 // 1, 10, 100, ... (place values)
for (digit in b.toString().reversed()) {
total += a * (digit - '0') * place // digit char -> Int
place *= 10
}
return total
}
fun main() {
println(multiplyByParts(125, 12)) // 1500
// 125*2*1 + 125*1*10 = 250 + 1250 = 1500
}
Practical
The right-triangle relationship in Kotlin
import kotlin.math.sqrt
// Check the right-triangle relationship: a^2 + b^2 == c^2
fun isRightTriangle(a: Int, b: Int, c: Int): Boolean {
return a * a + b * b == c * c
}
// Or compute the hypotenuse from the two shorter sides:
fun hypotenuse(a: Int, b: Int): Double {
return sqrt((a * a + b * b).toDouble())
}
fun main() {
println(isRightTriangle(3, 4, 5)) // true (9 + 16 = 25)
println(hypotenuse(3, 4)) // 5.0
println(isRightTriangle(3, 4, 6)) // false (9 + 16 = 25, not 36)
}
Quiz
The Kotlin function isRightTriangle(a, b, c) returns a*a + b*b == c*c. What does isRightTriangle(3, 4, 5) return, and why?
- false, because 3 + 4 is not 5
- true, because 3*3 + 4*4 = 9 + 16 = 25, and 5*5 = 25, so the equality holds
- An error
- true, but only by coincidence
Show the answer
true, because 3*3 + 4*4 = 9 + 16 = 25, and 5*5 = 25, so the equality holds
The function checks aa + bb == cc: for 3, 4, 5 that is 9 + 16 = 25 on the left and 55 = 25 on the right, so the equality holds and it returns true. This correctly implements the documented right-triangle relationship (known in the West as Pythagorean). Option A adds the sides instead of squaring (a common confusion the code avoids). Option C is wrong: the code runs fine. Option D undersells it: 3-4-5 satisfying the relationship is a genuine right triangle, correctly identified, not coincidence. The exercise shows how documented historical mathematics maps directly to a few lines of Kotlin, the language you just learned.
Think first
Why implement a method Kotlin could do with *?
Kotlin multiplies with a simple *, so why bother coding the Lilavati's part-by-part method? What is the educational value? Then tap.
Show the answer
The value is not to REPLACE the * operator but to UNDERSTAND and EXPRESS the historical method, and to connect two things: classical mathematics and your new Kotlin skills. Implementing the part-by-part multiplication forces you to understand HOW the method works (splitting by place value, summing partial products), which deepens your grasp of both the historical technique AND the distributive principle. It is the same educational reason you once implemented sorting by hand rather than calling a library: coding a method teaches the method. It also demonstrates that documented mathematics from centuries ago maps cleanly onto modern code, a nice bridge across time, and gives you practice in Kotlin. So the exercise is about comprehension and connection, not efficiency. To truly understand an algorithm, implement it.
Watch out
Implementation traps
Adding sides instead of squaring: the right-triangle check is aa + bb == c*c, not a + b.
c squared vs c: c*c is 25 for a 5-side; c itself is 5 (take the square root for the length).
Char to Int: converting a digit character needs (digit - '0'), not the char directly.
Float comparison: sqrt returns a Double; prefer the integer squared-form check (aa + bb == c*c) for exact equality.
Fabricating verse text in comments: keep comments factual; cite the method, not invented shlokas.
Theory
Methods coded; now the capstone project
You have implemented the Lilavati's documented methods in Kotlin, bridging historical mathematics and the language you just learned. The final topic of the whole subject is the capstone PROJECT: building a small Kotlin Android application based on Units 1 to 3. The next lesson gives guidance on scoping and building it well, tying together everything you learned in Advance Mobile Technology.
Summary
Key takeaways
- The syllabus asks you to implement the Lilavati's documented methods in code; this Kotlin subject uses Kotlin.
- The part-by-part multiplication (distributive method) splits the multiplier by place value and sums partial products: multiplyByParts(125, 12) = 1500.
- The right-triangle relationship is coded as aa + bb == c*c: isRightTriangle(3, 4, 5) is true (9 + 16 = 25).
- Compute the hypotenuse as sqrt((aa + bb).toDouble()): hypotenuse(3, 4) = 5.0.
- The value is understanding and connecting the historical method to code (and practising Kotlin), not replacing operators.
- Prefer the integer squared-form check over float comparison; keep comments factual (no fabricated verses).
- Memory hook: documented math maps to a few clean lines of Kotlin.