How many positive integers less than 1000000 have the sum of their digits equal to 7?
Details and assumptions
The digit sum of a number is the sum of all its digits. For example the digit sum of 1123 is 1 + 1 + 2 + 3 = 7 .
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
As a side note, this is a stars and bars problem only because the sum of digits is less than 9. Where/How do you implicitly use this?
If the question asked for the number of positive integers less than 1000000 which have sum of digits equal to 21, what would your answer be?
Log in to reply
It's a bit laborious, but we can find the total number of all solutions to a 1 + a 2 + . . . + a 6 = 2 1 , and then deduct the number of solutions when a i > = 1 0 .
When a i > = 1 0 , we can have at most 2 a i 's greater than 1 0 .
Case 1 : When there are two of them greater than equal to 1 0
The two a i 's can be chosen in ( 2 6 ) = 1 5 ways. Let those two elements be for instance say ( a 1 , a 2 ) . We let a 1 = a 1 ′ + 1 0 , a 2 = a 2 ′ + 1 0 .
So we need the number of non negative solutions for a 1 ′ + a 2 ′ + a 3 + a 4 + . . + a 6 = 1 . This is : ( 1 6 + 1 − 1 ) = 6
Total number of such solutions = 6 ⋅ 1 5 .
Case 2: When only one of them is greater than equal to 1 0 .
( 1 6 ) = 6 way to choose an a i .
Let, for instance, a 1 = a 1 ′ + 1 0
The number of solutions to a 1 ′ + a 2 + a 3 + . . . + a 6 = 1 1 is given by:
( 1 1 1 6 ) = 4 3 6 8 The required number of solutions = ( 2 1 2 1 + 6 − 1 ) − 9 0 − 4 3 6 8 = 6 5 7 8 0 − 9 0 − 6 ⋅ 4 3 6 8 = 3 9 4 8 2 .
Note: Whenever I say "solutions" I mean non-negative(integer) solutions.
EDIT: The second case double counts the cases in case 1. This is because in Case 2, we will also have two a i 's to be greater than equal to 1 0 . Once it will happen when we choose a i to be ≥ 1 0 , and this a i , when some other a i is ≥ 1 0 , can also be ≥ 1 0 . Number of solutions are:
6 5 7 8 0 − ( 6 ⋅ 4 3 6 8 − 9 0 ) = 3 9 6 6 2 .
Log in to reply
your case 2 overlaps some solutions with case 1.. one in which 2 numbers have value >=10 You need to substract those cases which comes out to be equal to 6C1 * 5C1 * 6
Log in to reply
@Yash Dalmia – Yeah, missed that. As a matter of fact, first case is not to be considered at all. They are all counted in the second case.
Log in to reply
@Aditya Parson – There is some problem in this. In the second case, the particular cases where two numbers have >10 value will be counted twice. (Think about it). How to do correct for that error?
Log in to reply
@Yash Dalmia – Ohh, I got the solution now. Case 2 minus Case 1 (as in your solution) will give us the number of cases to be substracted from the total number of cases. (since the case 2 counts the cases in 1 twice )
Log in to reply
@Yash Dalmia – Yeah, that's correct. I wrote a python program to cross check, got the same answer.
good question.. I also have this doubt. anyone with the answer??
How do you get the formula for n balls into k urns?
Note that every positive integers less than 1000000 can be written as 1 0 5 x 1 + 1 0 4 x 2 + 1 0 3 x 3 + 1 0 2 x 4 + 1 0 x 5 + x 6 , where x 1 , x 2 , x 3 , x 4 , x 5 , x 6 ∈ { 0 , 1 , 2 , … , 9 } . So the answer is equal to the number of non-negatif integer solutions of the equation x 1 + x 2 + x 3 + x 4 + x 5 + x 6 = 7 . That is ( 7 1 2 ) = 7 9 2 .
While you might be tempted to do casework on the number of digits, there's a clever way to simplify the problem.
Instead, modify the rules a little so that leading zeros still count towards the digit count (i.e., 0 0 0 0 0 1 is a 6 digit integer, not a 1 digit integer). Then, we are just looking to find the number of ways to have ordered 6-tuples of nonnegative integers add up to 7.
We will use a method called stars and bars, balls and urns, sticks and stones. Imagine that 7 is represented as 7 dots, and that we have 6 distinct boxes (5 dividers). We just have to find the number of ways to rearrange the 5 dividers in 12 spots, which is ( 5 7 + 5 ) = 7 9 2 .
This problem mainly requires careful counting:
Let's construct first some six or less digit numbers whose digit sum is seven. First, notice, for example, that 7 = 1 + 2 + 4 . Using this decomposition, we can create several numbers, not just 124 or 412, but we can add zeros to the number because it won't change the digit sum. So we can make 10024, or 4012. To total up the number or numbers we can make using this specific decomposition, we can use the multinomial coeffecient, or a simpler version as follows: We have six spaces, and we want to fill them up with a 1, 2, 4, 0, 0, and 0. We can place the zeros as the leading digits because the number can be shorter than six digits. So we have no restrictions on placing the numbers. From the multinomial theorem, one can easily find that the number of numbers we can create is 1 ! 1 ! 1 ! 3 ! 6 ! = 1 2 0 . The layman, however, could see that we have six places to put the 1, then five to put the 2, then four to put the 4, and then the rest of the places can be zero. Multiplying those up we get 6 ⋅ 5 ⋅ 4 = 1 2 0 .
However, one has to be careful when counting the layman's way. Say we use the decomposition 7 = 1 + 1 + 1 + 1 + 3 . Then we have six places to put the 3, five to put the first 1, four for the second 1, and so on until one spot is left for the zero. This is not the answer however, because we could swap any of the 1's and still have the same numbers. So a better way to think about it, more like the multinomial coeffecient, is that we have six places for the 3, and we can choose four of the remaining five places for the 1's. This gives us 6 ⋅ ( 4 5 ) = 3 0 , the right answer.
You can do this for all the various decompositions of 7, which I will list in case you missed any: 7 = 7 = 6 + 1 = 5 + 2 = 4 + 3 = 5 + 1 + 1 = 4 + 2 + 1 = 3 + 3 + 1 = 3 + 2 + 2 = 4 + 1 + 1 + 1 = 3 + 2 + 1 + 1 = 2 + 2 + 2 + 1 = 3 + 1 + 1 + 1 + 1 = 2 + 2 + 1 + 1 + 1 = 2 + 1 + 1 + 1 + 1 + 1 . Applying the methods detailed above and summing over all decompositions gives you a final answer of 792.
The largest number with its digit sum equals to 7 is 610,000. Hence, we only need to consider numbers with 6 digits or less. For the sum to be equal to 7, we can distribute 7 ones among the 6 places. However, in this case we allow the space to be equal to zero. Hence, this is similar to distributing the 7 identical ones into 6 distinct places, where the place can have no 'one' at all. Hence, the number of numbers with digit sum equal to 7 is (7+6-1)C(6-1)=792.
This problem can be simplified into this statement: Given all of the distinct sets of six not necessarily distinct integer elements whose sum is seven, how many permutations are there of each set? So, the first step is to find all such sets. These are:
{7, 0, 0, 0, 0, 0}
{1, 6, 0, 0, 0, 0}
{2, 5, 0, 0, 0, 0}
{3, 4, 0, 0, 0, 0}
{1, 1, 5, 0, 0, 0}
{3, 3, 1, 0, 0, 0}
{2, 2, 3, 0, 0, 0}
{1, 2, 4, 0, 0, 0}
{1, 1, 1, 4, 0, 0}
(2, 2, 2, 1, 0, 0}
{1, 1, 2, 3, 0, 0}
{1, 1, 1, 1, 3, 0}
{1, 1, 1, 2, 2, 0}
{1, 1, 1, 1, 1, 2}
Next, all we have to do is calculate. For the first set there are 5 ! 6 ! permutations, so 6. For the next three sets there are \frac {6!}{4!} permutations each, resulting in a total of 90. For the next three sets there are \frac {6!}{3!2!} permutations each, giving us 180 more. For the next set there are \frac {6!}{3!} permutations, giving us another 120. For the next two sets there are \frac {6!}{3!2!} permutations each, giving us 120. For the next set there are \frac {6!}{2!2!} permutations, or 180. For the next set there are \frac {6!}{4!} permutations, or 30. For the next set there are \frac {6!}{3!2!} permutations, or 60. And finally, for the last set there are \frac {6!}{5!} permutations, or 6.
Adding these all together, we come to our final answer, 792.
(I could have grouped the subsets a bit more, but I wanted to go through a logical order of of most 0's to least 0's)
We must examine 6 separate cases depending on how many digits the number is:
1-digit - There is only 1.
2-digit - There are 7 (16, 25, 34...70)
3-digit - I looked at the starting digit
1 - There are 7 (106, 115...160)
2 - There are 6 (205, 214...250)
3 - There are 5
4 - There are 4
5 - There are 3
6 - There are 2
7 - There is only 1 (700)
Giving a total of 7+6+5+4+3+2+1=28.
4-digit - I now looked at the starting 2 digits
10 - yields 7, but I noticed that starting with 11 would yield 6, 12 would yield 5, etc. giving me a total of 28, the same as the 3-digit result
20 - yields 6, so it's identical to the above result but starting with 6 giving a result of 6+5+4+3+2+1=21
Each successive number will start with 1 less than the previous, so 30 will result in 5+4+3+2+1=15, 40 will result in 10, 50 yields 6, 60 yields 3 and 70 yields only 1 giving a grand total of 28+21+15+10+6+3+1=84 4-digit numbers.
5-digit - I began the same as the 4-digit case but started to notice the recursive pattern. I started with the first 3 digits:
100 yields 7, 101 yields 6, etc. giving a total of 28
110 yields 6, 111 yields 5, etc. giving a total of 21
120 yields 5, 121 yields 4, etc. giving a total of 15
At this point, I noticed the pattern was the same as the 4-digit number sequence and would give me a total of 84.
200 yields 6, which would start the totals with 21 then 15, etc. giving a total of 21+15+10+6+3+1=56.
Again, we are repeating the previous pattern but starting with a progressively lower number, so 300 yields 15+10+6+3+1=35, 400 yields 10+6+3+1=20, etc.
The grand total of 5-digit numbers is 84+56+35+20+10+4+1=210
6-digit - I now started with the first 4 digits
1000 yields 84+56+35+20+10+4+1=210, the same as the 5-digit result
2000 yields 56+35+20+10+4+1=126
3000 yields 35+20+10+4+1=70
etc. giving a total of 210+126+70+35+15+5+1=462 6-digit numbers.
I then found the grand total, 1+7+28+84+210+462=792
The answer is 792.
Every number less than 1 0 6 can be written as : 1 0 5 a 1 + 1 0 4 a 2 + 1 0 3 a 3 + 1 0 2 a 4 + 1 0 1 a 5 + a 6 where 0 ≤ a i ≤ 9 .
Thus, we need to find the number of non-negative integer solutions to a 1 + a 2 + . . . + a 6 = 7
The starts and bars formula for finding all ordered pairs of non-negative integers a i ( 6 of them) , that sum to 7 is given by : ( 7 6 + 7 − 1 ) = ( 7 1 2 ) = 7 9 2
Here is a way to solve this in python: Set count = 0
for i in list(range(0,1000000)):
sum = 0
for x in list(range(len(str(i)))):
sum = sum + int(str(i)[x])
if sum == 7:
count = count + 1
Now print the count.
Log in to reply
Yeah... And the math? Every problem in this topic can be solved by a computer by brute force, but... well, that is not the idea of Brilliant.
First time we see this kind of problem, we can count it by digits possibilities (e.g. 1 digit, 2 digits, ...) as a promising way, but it waste minutes. So, how can we fasten it? The method i'd like to use is to count how many non-negative integers fulfilling
x 1 + x 2 + x 3 + x 4 + x 5 + x 6 = 7
as the numbers can't be all 0.
We need to know that positive integers less than 1000000 has at most 6 digits, hence we get 6 numbers to be added later. The sum of those numbers is 7. So, we need to find how many ordered pairs are there.
It's the same as we count (7+6-1)C(7) = 7 9 2 .
This is equivalent to the problem of counting the total number of 6 digit integers that allow zeros at the beginning and whose digits sum up to 7.
Let's consider the general problem of counting the total number of k digit integers whose digits sums up to n . We'll introduce a decision variable called a k n to denote precisely this value. Now, suppose we're looking for a 3 digit number whose digits sum up to 3. Well, we can start with 3 blank cells $$ \boxed{~} \boxed{~} \boxed{~} $$ In the leftmost box, we can either put in a 0 , 1 , 2 , or a 3 . Suppose we put a 1 there, then we now have the configuration $$ \boxed{1} \boxed{~} \boxed{~} $$ in order for all the digits to sum up to 3, it better be the case that the remaining unfilled boxes add up to 2. But without loss of generality, this becomes the problem of finding how many 2-digit numbers there are whose digits sum up to 3-1, or a 2 2 . Likewise, if we had filled a 2 in the leftmost box, like $$ \boxed{2} \boxed{~}\boxed{~} $$ then we need to find the number of 2-digit numbers whose digits sum up to 3 − 2 = 1 , which is denoted a 2 1 . In fact, we could sub in any number less than or equal to n into that first slot, so we get the equation $$ a {33} = a {20} + a {21} + a {22} + a_{23} $$
Furthermore, as long as the sum of the digits is less than 10, we also know that a 1 n = 1 , so this gives us the recurrence $$ \begin{array}{rl} a {1n} &= 1 \\ a {kn} &= \sum {j=0}^n a {k-1,j} \ \end{array} $$ Let's take this for a spin to see if we can see any patterns. $$ \begin{array}{rl} a {20} &= a {10} = 1 \\ a {21} &= a {10} + a {11} = 2 \\ a {22} &= a {10} + a {11} + a {12} = 3 \\ a {2n} &= n + 1 \\ a {3n} &= \sum {j=0}^n a {2j} \\ &= \sum {j=0}^n (j+1) \\ &= {n+2 \choose 2} \\ a {4n} &= \sum {j=0}^n a {3j} \\ &= \sum {j=0}^n {n+2 \choose 2} \\ &= {n+3 \choose 3} \\ a_{kn} &= {n+k-1 \choose k-1} \end{array} $$ So to answer the question, just find a 6 7 = ( 5 1 2 ) = 7 9 2
Note: this is combinatorially equivalent to the integer partition problem as well as the balls-and-urns problem, which admits the same relation.
We need to find the number of positive integers less than 1 0 0 0 0 0 0 whose digit sum is 7 .
Step 1 :We notice,all positive integers less than 1 0 0 0 0 0 0 have 6 digits or less.
Step 2 :Let the digits in 6th,5th,4th,3rd,2nd and unit's place be represented as x 1 , x 2 , x 3 , x 4 , x 5 , x 6 , where each of them may be equal to 0 ,but not all at once.
Step 3 :We are required to find all possible non-negative solutions of the equation: x 1 + x 2 + x 3 + x 4 + x 5 + x 6 = 7
Step 4 : The total no. of solutions of the equation is given by:
( 7 7 + 6 − 1 ) = ( 7 1 2 ) = 7 9 2
Note :The method used above to find the total no. of solutions is called "Stars and Bars Method".You can refer this.
Note that we can apply stars and bars here. We have 7 objects (1's) to distribute. Consider 6 digit numbers. We must distribute a one into the leftmost space in order to make sure that it does not star with a zero. There must be 5 dividers between the digits. Thus, the amount of ways we can do six, five, four,...., one digit numbers is ( 5 1 1 ) + ( 4 1 0 ) + ( 3 9 ) + ( 2 8 ) + ( 1 7 ) + ( 0 6 ) = 7 9 2 respectfully.
Perhaps we can simply calculate (7+6-1)C7= 792.
When I first saw this question about digit sum, the Stars and Bars technique first came into my mind. On a side note, since the question asks for positive integers less than 1 0 0 0 0 0 0 , all integers must have 6 or less digits.
Using the Stars and Bars technique, the question translates mathematically into a + b + c + d + e + f = 7 where ( a , b , c , d , e , f ) are non-negative integers, representing the digits of the integers satisfying the conditions. Note that the solution to this question is simply the number of ordered pairs of ( a , b , c , d , e , f ) .
Applying the Stars and Bars method, there are 5 bars to separate the integers into 6 digits, and 7 stars. Hence, we arrived at the answer: ( 5 7 + 5 ) = ( 5 1 2 ) = 7 9 2 .
We can rephrase the question to being in how many ways you can choose a , b , c , d , e , f ∈ ( 0 , 1 , 2 , . . . , 6 , 7 ) so that a + b + c + d + e + f = 7 . That question can then be rephrased to in how many ways can you divide 7 crosses with 5 lines. One possible arrangement is ∣ ∣ X X ∣ X ∣ X X X ∣ X which would then represent the number 0021301. Answering the last question is easy. Imagine we have 7 + 5 = 1 2 boxes and we toss the five line so that they land randomly in the boxes, only one in each box. In the rest of the boxes we then put crosses. The lines can be randomly tossed in ( 5 1 2 ) = 7 9 2 ways. Hence the answer is 792.
Solving this question, you can use the Stars and Bars (combinatorics) :
6 positions are available for the digits: * * * * * *
this can be reframed as the positive integer as well as 6-digits can not be 0. So, how many positive integer is ( k n + k − 1 ) = ( n − 1 n + k − 1 )
k is 7 as the sum of digits equal and n is 6 the star as the partition. We can count, ( k n + k − 1 ) = ( 7 6 + 7 − 1 )
( 7 1 2 ) = 7 ! 5 ! 1 2 !
7 9 2 .
I did it using multinomial,similar to the stars and bars method Reduces to 12C5
Notice that the problem is equivalent to finding a sequence of 6 digits that sum to 7 (notice that in a sequence, the first digit can be 0). So we have to find all nonnegative integer solutions to a 1 + a 2 + a 3 + a 4 + a 5 + a 6 = 7 . Now we can use distributions. If we have 7 objects and 5 dividers, scrambling it will give us 6 groups of objects. Thus, we have ( 5 1 2 ) = 7 9 2 .
6 positions are available for the digits :- - - - - -
Then the question can be re-framed as "non-negative integers" as all 6 digits can't be 0
Now, using the stars and bars formula, Number of integers = (7+6-1)C(6-1) = 12C5 = 792
6ta digit..so a+b+c+d+e+f=7 er number of non negative solutions holo answer.
Which is 12C5=792
Can you explain your reasoning?
since 12C7 = 12C5
We'll just assume the number to be a b c d e f just like 1 2 3 4 5 6 .
According to the question, a + b + c + d + e + f = 7
Since it can be a one-digit or two-digit number which means a , b , c , d , e , f , g , h any of them can be equal to 0 .....
So we need to find solutions for a + b + c + d + e + f = 7
Formula - C ( n + r − 1 , r − 1 ) where n = 7 , r = 6
Hence we get the answer as 7 9 2
more words than numbers....
Log in to reply
Having more words than numbers is perfectly okay in a math proof. The point of a solution is to communicate the answer to other people, as well as the way of getting the answer. Words are great for communicating to other people.
If a solution only has numbers and symbols, it's often hard to follow. Adding words makes it more readable and less dense. Just as it is possible to use too few words, it is possible to use too many words. Finding the right balance is sometimes tricky.
The number of k with k ∈ Z + , k < 1 0 0 0 0 0 0 , S ( k ) = 7 can be found by splitting into six cases by the number of digits.
Case 1: 6 digits We are simply finding the number of solutions to x 1 + x 2 + ⋯ + x 6 = 7 , where the x i ′ s are the digits in order. Since x 1 > 1 , we must subtract 1 from the RHS, yielding x 1 + x 2 + ⋯ + x 6 = 6 which the number of solutions to can be easily found with stars and bars . Applying the formula, we have ( n n + k − 1 ) ⟹ ( 6 6 + 6 − 1 ) = ( 6 1 1 ) = 4 6 2 desired numbers.
Case 2: 5 digits Similarly, our equation is x 1 + x 2 + ⋯ + x 5 = 6 which gives d b i n o m 1 0 6 = 2 1 0 numbers.
We continue on with each of the cases for a final answer of ( 6 1 1 ) + ( 6 1 0 ) + ( 6 9 ) + ( 6 8 ) + ( 6 7 ) + ( 6 6 ) = 4 6 2 + 2 1 0 + 8 4 + 2 8 + 7 + 1 = 7 9 2 numbers .
I love stars and bars problems :D
Represent each "unit" we want by a star; there are 7 such stars (representing the 7 that the digits must add up to). Additionally, all of the numbers must be less than 700,000, so we put 5 bars into a mixture with 7 stars. These bars separate the 7 stars into 6 digits. There are 7 ! 5 ! ( 7 + 5 ) ! = 7 9 2 ways to arrange this mixture of stars and bars, and in this manner we arrive at our desired integers.
Problem Loading...
Note Loading...
Set Loading...
This problem is equivalent to finding the number of ways you can drop 7 balls into 6 urns. The 7 balls represent the sum of the digits, and the 6 urns represent the number of available place value spots.
The number of ways to drop n balls into k urns is ( n k + n − 1 ) . Setting k = 6 and n = 7 , there are ( 7 1 2 ) integers < 1 0 0 0 0 0 0 with the sum of their digits equalling 7 .
( 7 1 2 ) = 7 ! 5 ! 1 2 ! = 5 × 4 × 3 × 2 1 2 × 1 1 × 1 0 × 9 × 8 = 1 1 × 9 × 8 = 7 9 2
This technique is also called "stars-and-bars".