Skip to content

Instantly share code, notes, and snippets.

@joriki
Created April 11, 2016 22:03
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/976f61f0c242092d320a66c5566e6f29 to your computer and use it in GitHub Desktop.
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.
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