Anyone can get the equation for this? I couldn’t
class Calculator {
public int count = 0;
public void calc(int n, int p) {
count++;
if (p>n) return;
for (int i=0; i<n; i++) {
calc(n, p+1);
}
}
}
// int n is input by keyboard
Calculator c = new Calculator();
c.calc(n, 0);
System.out.println(c.count);
Anybody with the equation or any information?
The
countis incremented once and then calc is calledntimes, this recurses1 + ntimes due to thep > ntest. BTW If it werep >= nit would recursentimes.The equation is
where the expression
1 + nappears1 + ntimes.e.g. calc(3,0) = 121 =