1) The first 6 triangle numbers are 0, 1, 3, 6, 10, 15. The nth triangle number is 1 + 2 + 3 + … + n. Write a recursive method to find the nth triangle number
```java
int triangle(int n)
{
if (n == 1) return 1;
return n + triangle(n-1);
}
```
2) Write a recursive method that returns m to the nth power, e.g. 2 to the power of 3 returns 8.
```java
int mToTheN(int m, int n)
{
if (n == 0) return 1;
if (n == 1) return m;
return m * mToTheN(m, n-1);
}
```
3) The Harmonic Series begins 1 + 1/2 + 1/3 + 1/4 + 1/5 + … Write a recursive method that finds the Harmonic Series up to the nth term.
```java
double harmonic(double n)
{
if (n == 1) return 1;
return 1.0/n + harmonic(n-1);
}
```
4) Write a recursive method that counts the number of times "hi" appears in a string. Example countHi("hi ho, hi ho. It's high time… " will return 3.
```java
int countHi(String s)
{
if (s.length()<2)
return 0;
else if (s.substring(0,2).equals("hi"))
return 1 + countHi(s.substring(1, s.length()));
else
return countHi(s.substring(1, s.length()));
}
```
5) The Fibonacci Series begins 1,1,2,3,5,8,13,… The next digit is the sum of the last two digits, so 1 + 1 = 2, 1 + 2 = 3 etc. Write a recursive method to print the nth fibonacci number
```java
int fibonacci(int n)
{
if (n == 1) return 1;
if (n == 2) return 2;
return fibonacci(n-1) + fibonacci(n-2);
}
```
6) Use a recursive method to help check if string is a palindrome
```java
String reverseH(String s)
{
if (s.length() == 0) return "";
else if (s.length() == 1) return s;
else return reverseH(s.substring(1)) + s.substring(0,1);
}
boolean isPalindrome(String s)
{
return s.equals(reverseH(s)) ? true : false;
}
```
7) Write a recursive method called reduce() that returns a String with repeated characters removed. Example: reduce("mmmississippi") returns "misisipi"
```java
if (s.length() == 1) return s;
if (s.charAt(0) == s.charAt(1))
{
return reduce(s.substring(1));
}
else
{
return s = s.charAt(0) + reduce(s.substring(1));
}
```