How Many N-Digit Numbers Have Sum S?

In summary, the formula for calculating the amount of n digit numbers with sum s is given by:\sum_{r=0}^{\lfloor s/m \rfloor} \, (-1)^r \, C(n,r) \, C(s+n-1-m r,n-1) This formula is derived from the standard combination formula and takes into account the restriction that at most m items can be chosen from each category.
  • #1
T1000
7
0
I want to calculate the amount of n digit numbers with sum s.
The idea was the number of ways to put s balls in n boxes. So there are C(n+s-1,n-1) ways to do that when n>1 and s<10. But this formula doesn't work when s>10.
I found a formula C(10n-s-1,n-1) that works for cases where s>=10 but I can't figure out how its obtained.
 
Mathematics news on Phys.org
  • #2
Do you mean with digit sum s?
 
  • #3
Yes..
eg: 231 is a 3 digit number with sum 6.
 
  • #4
T1000 said:
I want to calculate the amount of n digit numbers with sum s.
The idea was the number of ways to put s balls in n boxes. So there are C(n+s-1,n-1) ways to do that when n>1 and s<10. But this formula doesn't work when s>10.
I found a formula C(10n-s-1,n-1) that works for cases where s>=10 but I can't figure out how its obtained.

Can you please explain why you think that formula doesn't work for s>10?

C(n+s-1,n-1) gives the number of way of choosing s items from n catagories, where ordering of choices is unimportant and where you may choose from any category zero or more times. This is in no way limited to s less than 10.
 
  • #5
BTW. An example of the use of this formula might be something like the following :

Q. A shop sells three kinds of soft-drink, Lemonade, Coke and Ginger-Beer. Your are sent to the shop to buy a round of eleven drinks, in how many different ways can you do this?

A. Clearly the ordering is unimportant. That is, a round consisting of 7 cokes, 3 lemonade and 1 GB is clearly the same as one consisting of 3 lemonade 1 GB and 7 cokes etc. So C(n+s-1,n-1) is the appropriate formula to use.

Here n = 3 (number of categories) and s = 11 (number of items). So C(13,2) = 78 combinations of drinks.

Note that I just used the formula with s>10
 
  • #6
I'm trying to get a formula to calculate the amount of n digit numbers with sum s.
The maximum value a digit can have is 9. When s>=10, using this formula the value of a digit can exceed 9.
 
  • #7
Ok, I thought it was all originally in relation to the "The idea was the number of ways to put s balls in n boxes" problem. In that case I think it was appropriate.

Yeah I see what you mean now, it was the digit sum thing that was the original problem and the ball in boxes thing was just the first attempt at solving it. My misunderstanding.
 
Last edited:
  • #8
So I think we can still reduce it to a "choose items from catagories" type of problem. I think the following is equivalent.

Choose s items from n catagories with ordering unimportant but the restriction that at most 9 items (0..9) can be chosen from any given category.

I don't know the solution off-hand, but the problem re-statement might help.
 
Last edited:
  • #9
I'm not 100% sure if this is correct but I just scratched out a quick solution and I think the following might work for s>=10

C(s+n-1,n-1) - n C(s+n-11,n-1)

The second term is supposed to subtract off all the combinations in which 10 or more items can get into a single category.
 
  • #10
T1000 said:
I found a formula C(10n-s-1,n-1) that works for cases where s>=10 but I can't figure out how its obtained.

That formula is definitely incorrect. It's a little tedius but you can enumerate all the possiblilties for a small example like s=11 and n=3 and you get 8+9+10+9+8+7+6+5+4+3 = 69.

That formula gives C(18,2) = 153 so it must be wrong.

BTW. My solution gives C(13,2) - 3*C(3,2) = 69. It's correct for this case though I'm still not certain it works for all cases. I'm fairly certain it will work up to s=19, but I've got the feeling it might under-estimate the correction term (subtracted) when s is large enough to "overflow" more than one category, that is when s>=20.
 
Last edited:
  • #11
You're right. Your formula does not work for cases where s>=20.
The formula C(10n-s-1,n-1) only works in cases where (10n-s) is generally less than 15.
Is this is a difficult problem? I thought it was an easy one which I couldn't do because I'm not strong in math.
 
  • #12
Maybe my approach was not right. Maybe we should try it from some other direction or something.
 
  • #13
No one has other ideas? :confused:
 
  • #14
T1000 said:
No one has other ideas? :confused:

Yes I think I've cracked it.

Problem.

How many combinations (order unimportant) are there of "s" items drawn from "n" categories, when we can choose at most "m" items from each category.

Result.

[tex]\sum_{r=0}^{\lfloor s/m \rfloor} \, (-1)^r \, C(n,r) \, C(s+n-1-m r,n-1) [/tex]

Where [itex]C(n,r)[/itex] is the usual combination function,
[tex]C(n,r) = \frac{n!}{r!\,(n-r)!}[/tex]

BTW. This is an extension of my first attempt which was equivalent to the first two terms of this summation.
 
  • #15
Can you please tell me how you got this formula?
 
  • #16
T1000 said:
Can you please tell me how you got this formula?

My working was pretty messy and I haven't formally proved it, but I can give a rough outline of how I arrived there.

I started with the standard proof of the C(s+n-1,n-1) formula, for the case where there is no limit on the number drawn from each category. Are you familiar with this method where you represent each possible combination as a different binary code? Take for instance the 11 soft drink problem I posted earlier when I gave the example of an order placed for 7 cokes, 3 lemonade and one ginger-beer. Once we agree on the order of the categories as for example "coke,lemon,ginger" then that particular order can be encoded as the binary number "0000000100010". The "s" zeros denote the repeated items in each category while the "n-1" ones represent the category boundaries. So in this case every different binary number constructed from 11 zeros and 2 ones represents a different possible round of 11 drinks and it's simply a matter of noting that there are C(s+n-1,n-1) different ways of placing the "n-1" ones.

Next I tried to account for the restriction that at most 9 items could be chosen from any given category (and later I generalized this from 9 to m). First I attempted to subtract away (from the earlier result) the number of combinations where a given category had 10 or more items. At first I just considered the case where s<=19 so that at most one category could equal or exceed 10 items. I imagined removing 10 zeros from the binary number and then forming all the combinations that I could with the remaining "s-10" zeros and "n-1" ones, then multiplying that by the number of ways that I could re-insert the block of 10 zeros. That's how I arrived at the C(s+n-1,n-1) - n C(s+n-11,n-1) formula.

When I analysed why this failed for n>=20 I realized that the "n C(s+n-11,n-1)" term incorrectly counted "the number of combinations where a given category had 10 or more items" when two or more categories each exceed 10 items. Eventually I worked out the correction that I needed to add to make the result hold for s<=29 (that is, a maximum of two categories exceeding 10 items) was to add C(n,2) C(s+n-21,n-1). I basically just proceded like this until I saw the pattern.
 

FAQ: How Many N-Digit Numbers Have Sum S?

What is an "N digit number with sum s"?

An "N digit number with sum s" refers to a number that has N digits and when you add up all the digits, the sum is equal to s. For example, a 3 digit number with sum 10 could be 145, where 1+4+5 = 10.

How do you find all possible "N digit numbers with sum s"?

To find all possible "N digit numbers with sum s", you can use a combination formula. The formula is (s+N-1) choose (N-1), where s is the sum and N is the number of digits. For example, if you want to find all 3 digit numbers with a sum of 10, the formula would be (10+3-1) choose (3-1) = 12 choose 2 = 66 possible numbers.

Can an "N digit number with sum s" be a negative number?

No, an "N digit number with sum s" cannot be a negative number. It must be a positive integer with the specified number of digits and the sum of its digits must also be positive.

What is the largest possible "N digit number with sum s"?

The largest possible "N digit number with sum s" is when all the digits are 9. For example, the largest 3 digit number with a sum of 10 would be 999, where 9+9+9 = 27. It is not possible to have a larger "N digit number with sum s" because the sum of the digits would exceed s.

Can an "N digit number with sum s" have repeating digits?

Yes, an "N digit number with sum s" can have repeating digits. As long as the sum of the digits is equal to s and the number of digits is N, the number is considered an "N digit number with sum s". For example, 112 is a 3 digit number with a sum of 4, as 1+1+2 = 4.

Similar threads

Replies
2
Views
2K
Replies
3
Views
1K
Replies
7
Views
2K
Replies
5
Views
2K
Replies
2
Views
1K
Replies
2
Views
1K
Back
Top