/* * Compute factorials recursively. * One on the way into the recursion, once on the way out * In is often more efficient, but requires can require extra parameters * @author ggtowell * Created: Oct 27, 2023 */ public class Factorial { public static void main(String[] args) { factSending(5, 1); factReturning(5); } /** * Recursively on the way out * @param num * @return */ public static int factReturning(int num) { if (num == 1) { return 1; } return num * factReturning(num - 1); } /** * Recursively on the way in * @param num * @param res * @return */ public static int factSending(int num, int res) { if (num == 1) { return res; } return factSending(num - 1, res * num); } }