Yes.
Triangle
is so easy that you don't need to use either
iteration or recursion for it.
You might recognize Triangle(N)
as the sum the first N
integers:
1 + 2 + 3 + ... + (N-1) + N = (N * (N+1))/2
For example
1 + 2 + 3 + 4 = (4*5) / 2 = 10
Of course, implementing this formula in Java is simple. And that is how you would do it in a practical situation. But the goal of this chapter was to show recursion with Java and triangle numbers work well for that.
Are you ready for another example?