3

I have the following snipet :

public class A

public A(int n)
{
    for (int i=0; i<n; i++)
    {
        new A(i)
    }
    console.writeln("?")
}

for a given n - how many "?" will be printed ?

  • some testing shows the answer is 2^n. What is the way to reach the formula?
  • I got to the formula F(n) = 1 + F(n-1) +... +F(1) + 1

how do I simplify it to 2^n ?

Mitch Dempsey
  • 38,725
  • 6
  • 68
  • 74
Elad Benda
  • 35,076
  • 87
  • 265
  • 471
  • It's better to write these sort of things as functions, not constructors ;-) –  Sep 05 '11 at 19:04
  • how can I move it to S/E - math – Elad Benda Sep 05 '11 at 19:17
  • 3
    This question appears to be off-topic because it is not asking for a solution to a programming problem, but rather for a computational analysis of the code for academic reasons. It would be better asked at [programmers.stackexchange.com](http://programmers.stackexchange.com). – Matt Johnson-Pint Mar 02 '14 at 22:09

1 Answers1

2

If you cannot simplify it, perhaps you can prove it by induction.

http://en.wikipedia.org/wiki/Mathematical_induction

I don't know if this question is pertinent here... it has more to do with maths than with programming.

Miguel Angelo
  • 23,796
  • 16
  • 59
  • 82
  • how can I move it to S/O - math ? – Elad Benda Sep 05 '11 at 18:57
  • I don't know, just guessing... I have looked now, and it seems there are lots of questions about math on StackOverflow... I guess you can ask this kind of question here. =) – Miguel Angelo Sep 05 '11 at 19:41
  • So how would I get to the 2^n the first place ?I mean what if it was a harder pattern than: 0, 1, 2, 4, 8 ? – Elad Benda Sep 05 '11 at 20:07
  • That is the point of induction... you don't have to get to 2^n by any mathematical means, *it's a guess, you guess it may be 2^n*... then you try to prove that your guess is correct by using mathematical induction... and finally proving that they are the same. If the guess is not correct, then you will get invalid output from the induction, such as 1 = 0, or some other impossible result. – Miguel Angelo Sep 05 '11 at 20:33