Question: Implement two functions in Scala that take two integers inputs x and y and compute the sum of all values between the two, inclusive. (If
Implement two functions in Scala that take two integers inputs x and y and compute the sum of all values between the two, inclusive. (If x = y, this result will be x.) To do so, you may want to import scala.math._ to use its min or math functions. Other than that, you can use elementary arithmetic.
-
Your first version, called rangeSum, should work iteratively, using some var and loop.
-
Your second version, called rangeSumRec, should work recursively. That is, there must be
no use of var and no looping.
Example Output:
println(rangeSum(-1, -1)) // -1 println(rangeSum(1, 1)) // 1 println(rangeSum(-1, 1)) // 0 println(rangeSum(1, -1)) // 0 println(rangeSum(1, 5)) // 15 println(rangeSum(5, 1)) // 15 println(rangeSum(-1, -5)) // -15 println(rangeSum(1, -5)) // -14
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
