object RightTriangle {
def main(args: Array[String]): Unit = {
val perimeter = 120.0
val legA = 20.0
// From perimeter: a + b + c = P
// From Pythagoras: c = sqrt(a^2 + b^2)
// Substitute c into perimeter equation: a + b + sqrt(a^2 + b^2) = P
// Solve for legB
val legB = (math.pow(perimeter - legA, 2) - math.pow(legA, 2)) / (2 * (perimeter - legA))
if (legB <= 0) {
Console.err.println("No valid right triangle exists with these values.")
sys.exit(1)
}
val hypotenuse = math.sqrt(legA * legA + legB * legB)
println(f"Leg a: $legA%.3f")
println(f"Leg b: $legB%.3f")
println(f"Hypotenuse c: $hypotenuse%.3f")
}
}
/*
run:
Leg a: 20.000
Leg b: 48.000
Hypotenuse c: 52.000
*/