/* Problem151.java CIS 160 David Klick 2015-09-11 Solves Project Euler problem 151 */ public class Problem151 { static int count = 0; public static void main(String[] args) { System.out.printf("%8.6f\n", f(1,1,1,1)); } private static double f(int a, int b, int c, int d) { if (a+b+c+d==1) { if (d==1) return 0.0; if (a==1) return 1.0 + f(0,1,1,1); if (b==1) return 1.0 + f(0,0,1,1); else return 1.0 + f(0,0,0,1); } else { double tot = a + b + c + d; double result = 0.0; if (a > 0) result += (a / tot) * f(a-1,b+1,c+1,d+1); if (b > 0) result += (b / tot) * f(a,b-1,c+1,d+1); if (c > 0) result += (c / tot) * f(a,b,c-1,d+1); if (d > 0) result += (d / tot) * f(a,b,c,d-1); return result; } } } /* Output: 0.464399 */