package com.codingbat.recursion1;

/**
 * The class Recursion1 is contains solution for Recursion-1 section.
 */
public class Recursion1 {

  /**
   * Given n of 1 or more, return the factorial of n, which is n * (n-1) * (n-2) ... 1. Compute the
   * result recursively (without loops).
   *
   * @param n the input number
   * @return the above mentioned new number
   */
  int factorial(int n) {
    if (n == 0 || n == 1) {
      return 1;
    }
    return n * factorial(n - 1);
  }

  /**
   * We have a number of bunnies and each bunny has two big floppy ears. We want to compute the
   * total number of ears across all the bunnies recursively (without loops or multiplication).
   *
   * @param bunnies the number of bunnies
   * @return the above mentioned new number
   */
  int bunnyEars(int bunnies) {
    if (bunnies == 0) {
      return 0;
    }
    return 2 + bunnyEars(bunnies - 1);
  }

  /**
   * The fibonacci sequence is a famous bit of mathematics, and it happens to have a recursive
   * definition. The first two values in the sequence are 0 and 1 (essentially 2 base cases). Each
   * subsequent value is the sum of the previous two values, so the whole sequence is: 0, 1, 1, 2,
   * 3, 5, 8, 13, 21 and so on. Define a recursive fibonacci(n) method that returns the nth
   * fibonacci number, with n=0 representing the start of the sequence.
   *
   * @param n the fibonacci index
   * @return the above mentioned new number
   */
  int fibonacci(int n) {
    if (n == 0) {
      return 0;
    }
    if (n == 1) {
      return 1;
    }
    return fibonacci(n - 1) + fibonacci(n - 2);
  }

  /**
   * We have triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks,
   * the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the
   * total number of blocks in such a triangle with the given number of rows.
   *
   * @param rows the rows of triangles
   * @return the above mentioned new number
   */
  int triangle(int rows) {
    if (rows == 0) {
      return 0;
    }
    return rows + triangle(rows - 1);
  }

  /**
   * Given a non-negative int n, return the sum of its digits recursively (no loops). Note that mod
   * (%) by 10 yields the rightmost digit (126 % 10 is 6), while divide (/) by 10 removes the
   * rightmost digit (126 / 10 is 12).
   *
   * @param number the input number
   * @return the above mentioned new number
   */
  int sumDigits(int number) {
    int rightmostDigit = number % 10;
    int rightmostRemoved = number / 10;
    if (rightmostRemoved == 0) {
      return rightmostDigit;
    }
    return rightmostDigit + sumDigits(rightmostRemoved);
  }
}

@@ -305,24 +305,29 @@
   * @param n
+umber
  the input n
@@ -402,24 +402,29 @@
 torial(int n
+umber
 ) %7B%0D%0A    if 
@@ -421,24 +421,29 @@
 %7B%0D%0A    if (n
+umber
  == 0 %7C%7C n =
@@ -440,16 +440,21 @@
 = 0 %7C%7C n
+umber
  == 1) %7B
@@ -491,16 +491,21 @@
 return n
+umber
  * facto
@@ -510,16 +510,21 @@
 torial(n
+umber
  - 1);%0D%0A
@@ -1447,17 +1447,30 @@
  @param 
-n
+fibonacciIndex
  the fib
@@ -1559,23 +1559,49 @@
 int 
-n) %7B%0D%0A    if (n
+fibonacciIndex) %7B%0D%0A    if (fibonacciIndex
  == 
@@ -1638,17 +1638,30 @@
     if (
-n
+fibonacciIndex
  == 1) %7B
@@ -1707,17 +1707,30 @@
 bonacci(
-n
+fibonacciIndex
  - 1) + 
@@ -1739,17 +1739,30 @@
 bonacci(
-n
+fibonacciIndex
  - 2);%0D%0A

rename variables.