/* File: BadRec.java - March 2018 */ package l06; /** * Illustrating the horrors that arise when you have recursion inside loops! * The recursive function we're trying to compute is defined by: * * B(1) = 1 * B(n) = 1*B(1) + 2*B(2) + ... + (n-1)*B(n-1) * * Sample usage: java l06.BadRec 3 * * @author Michael Albert * */ public class BadRec{ static int calls = 0; public static void main(String[] args) { long n = Long.parseLong(args[0]); System.out.println("B(" + n + ") = " + b(n)); System.out.println("Calls required " + calls); } public static long b(long n) { calls++; if (n == 1) return 1; long result = 0; for(int i = 1; i < n; i++) { result += i*b(i); } return result; } }