Question: Given the triangle number sequence: 1, 3, 6, 10, 15, 21, 28, 36, 45, The recursive triangle function below calculates the triangle number at term
Given the triangle number sequence: 1, 3, 6, 10, 15, 21, 28, 36, 45,
The recursive triangle function below calculates the triangle number at term n where n is assumed to be greater than 0 (i.e. n starts at 1). However, there is something wrong with the recursive triangle Java function below. For example, if you call the function triangle(3);, you would expect it to return 6 (because it is the 3rd term in the sequence), but this function doesnt.
a) If the current incorrect function was called as int result = triangle(4);, what would the value be in result?
b) What code change(s) would you make to fix the triangle function?
// Assume term > 0 public int triangle(int term) { if (term == 1) return term; return term * triangle(term 1); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
