beyondgrader.com Logo
DemoBrowseAboutTeamLogin

Recursive Range Sum

Geoffrey Challen // 2021.10.0

Create a class RangeSum with a public static method sum that accepts a single int value and returns the sum of all the integers in the range 1..value as an int. So, for example, given the input 10 you should return 55: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10. You can reject arguments less than or equal to 0 and ones greater than 128 by throwing an IllegalArgumentException.

You should submit a recursive solution. The range sum of 1 is 1, and this represents the base case. The range sum of n is n + the range sum of n - 1, and this represents the recursive step.