Created
April 11, 2016 22:03
-
-
Save joriki/976f61f0c242092d320a66c5566e6f29 to your computer and use it in GitHub Desktop.
Count the rolls of n dice that allow a sum of s to be formed; see http://math.stackexchange.com/questions/1737961.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Question1737961 { | |
final static int nsides = 6; | |
public static void main (String [] args) { | |
for (int n = 3;n <= 5;n++) { | |
for (int s = 2;s <= nsides + 1;s++) | |
System.out.println (n + ", " + s + " : " + recurse (new int [n],0,s)); | |
} | |
} | |
static int recurse (int [] a,int i,int s) { | |
if (i == a.length) { | |
for (int j = 1;j < a.length;j++) | |
for (int k = 0;k < j;k++) | |
if (a [j] + a [k] == s) | |
return 1; | |
return 0; | |
} | |
int sum = 0; | |
for (a [i] = 1;a [i] <= nsides;a [i]++) | |
sum += recurse (a,i + 1,s); | |
return sum; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment