This question already has an answer here:

Challenge: given a number, calculate how many unique, order-independent ways there are to achieve a score of that number in American Football.

Let's see how few characters can be used in each of your favorite languages! The usual rules apply. Input and output in a way that is natural to your language.

Recall that the ways to score in Football are safety (2 points), field goal (3 points), touchdown (6 points), touchdown + extra point (7 points), and touchdown + 2-point conversion (8 points).

For example, given 9 as input, the program should return 4, since you could score:

  • 1 touchdown + extra point and 1 safety
  • 1 touchdown and 1 field goal
  • 3 field goals
  • 3 safeties and 1 field goal

Note that order does not matter.


marked as duplicate by Peter Taylor, John Dvorak, Justin, manatwork, Timtech Jan 8 '14 at 15:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    \$\begingroup\$ I'm not familiar with American Football. Is the question just about ways of partitioning the number using 2,3,6,7 and 8? \$\endgroup\$ – swish Jan 6 '14 at 22:35
  • \$\begingroup\$ @swish yup! Just an actual version of the problem I found myself thinking about. \$\endgroup\$ – Ben Reich Jan 6 '14 at 22:43
  • 4
    \$\begingroup\$ This is essentially identical to Need change of 78 cents except for the values of some constants. (Although on reflection, since this question is better posed, it might be preferable to close the other one as a duplicate of this one). \$\endgroup\$ – Peter Taylor Jan 6 '14 at 23:48
  • \$\begingroup\$ Awhh. I watch the Canadian Football League. I want my 1 point for punting into the endzone! \$\endgroup\$ – Cruncher Jan 7 '14 at 14:22

Mathematica - 42


Prolog - 140

Never wrote anything in Prolog before, just knew that this problem should be quite nice for it.

p(N,[H|T],O):- N>=H,p(N,T,O). 
p(N,[H|S],[H|T]):- N>=H,M is N-H,p(M,[H|S],T).
f(N,C):- aggregate(count,X^p(N,[2,3,6,7,8],X),C).

?- f(9,C).
C = 4.
  • \$\begingroup\$ I only see 42 chars. Does this have something to do with the unicode character? \$\endgroup\$ – Cruncher Jan 7 '14 at 14:25
  • \$\begingroup\$ @Cruncher Oh, I forgot to put f= at the start, so it would be a function definition. \$\endgroup\$ – swish Jan 7 '14 at 15:23

Python - 146

from itertools import*
print len([a for a in chain(*(combinations_with_replacement([2,3,6,7,8],n) for n in range(t)))if sum(a)==t])
  • \$\begingroup\$ Instead of from itertools import ..., you can use from itertools import*. You only use combinations_with_replacement once, so having to use it wouldn't cost you after this change. \$\endgroup\$ – Konrad Borowski Jan 7 '14 at 13:19
  • \$\begingroup\$ True, I'll change it, thanks! \$\endgroup\$ – aquavitae Jan 7 '14 at 14:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.