Skip to content

Instantly share code, notes, and snippets.

@joriki
Created June 30, 2018 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joriki/6a1e36f4de7118076a26c423200780e6 to your computer and use it in GitHub Desktop.
Save joriki/6a1e36f4de7118076a26c423200780e6 to your computer and use it in GitHub Desktop.
Calculate the expected number of elements in a rooted ordered unlabeled binary tree of maximum depth d; see https://math.stackexchange.com/questions/2836730
import java.math.BigInteger;
public class Question2836730 {
public static void main (String [] args) {
BigInteger a = BigInteger.ONE;
BigInteger s = BigInteger.ZERO;
for (int d = 1;;d++) {
BigInteger square = a.multiply (a);
BigInteger sn = s.multiply (a).multiply (BigInteger.TWO).add (square);
BigInteger an = square.add (BigInteger.ONE);
BigInteger ds = sn.subtract (s);
BigInteger da = an.subtract (a);
double mean = ds.doubleValue () / da.doubleValue ();
System.out.println (d + "&" + sn.doubleValue () / an.doubleValue () + "&" + mean + "&" + (mean + 1) / Math.pow (2,d) + "\\\\");
a = an;
s = sn;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment