How many integers between 1 and 1,000,000 have the sum of their digits equal to 18?
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.
Why you said that the total number of solutions for the first equation is 23C18 ?
Log in to reply
http://en.wikipedia.org/wiki/Stars and bars_(combinatorics)
With using generating function, the 18-sum-six-digit number can be expressed by the coefficient of its 18-powered term. Because of each digit must be either 0 to 9 , and we are considering the six-digit number; the generating function then is f ( x ) = ( 1 + x + x 2 + . . . + x 8 + x 9 ) 6 That is, f ( x ) = ( 1 − x 1 − x 1 0 ) 6 which can be explicitly stated as f ( x ) = ( 1 + r = 1 ∑ 6 ( − 1 ) r C 6 , r x 1 0 r ) ( 1 + r = 1 ∑ ∞ C r + 6 − 1 , r x r ) We eventually get the answer, i.e. the coefficient of 18-powered x term, equal to C 2 3 , 1 8 − 6 ∗ C 1 3 , 8 = 2 5 9 2 7
ps. I failed with my wrong calculating skill in first two tries. LOL
Log in to reply
can u give a link for more information on Generating Function??
Cool! I've done a completely different solution for this one, luckily I got it right on my first attempt! I'm going to post it here!
Lazy Pythonic approach:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
we have to consider integers less than 999,999.I will here take an example like 230661.This number satisfies the criteria that is sum of digits is 18.Now I will represent this integer as a notation of D's and s.Put number of D's corresponding to value of digit separated by stars which will mean spaces. So 230961=DD DDD *DDDDDD DDDDDD D.Now what I have to do now is arrange these Ds and stars.Total arrangement=23c5.There will be some bad integers where number of consecutive Ds will be 10 or more,because there is no digit as 10.So I will minus these cases which will be 6 13c5 . So numbers=23c5-6*13c5
1 2 3 4 5 6 7 8 9 10 11 12 |
|
int sum(int n){ int x=0; while(n){ x+=n%10; n/=10; } return x; } int main(){ int x=0; for(int i = 99;i<1000000;i++){ if(sum(i)==18){ x++; } } cout<<x; }
Problem Loading...
Note Loading...
Set Loading...
We can think of the solutions of this problem as numbers with 6 digits, since they are between 1 and 1 , 0 0 0 , 0 0 0 . So we are searching the solutions from the following equation: x + y + z + t + w + u = 1 8 . The total of solutions for this equation is: ( 1 8 2 3 ) = 3 3 6 4 9 .
An example for this solution is:
( 0 , 0 , 1 , 3 , 5 , 9 ) , which is equivalent to the number: 1 3 5 9 .
Now we have to take out the solutions which contain digits from 1 0 to 1 8 . We have to choose one of the variables to be one of these numbers above, so we have 6 options to set each of them. Now we have to multiply by 6 the total of solutions from the following inequality: x + y + z + t + w ≤ 8 . Now comes the trick : the number of solutions of this inequality is equivalent to the number of solutions from the following equation: x + y + z + t + w + u = 8 . Notice that, by introducing an extra variable, you insert solutions from 0 to 8 , which is equivalent to the inequality above except by one variable.
The total of solutions from this equation is: ( 8 1 3 ) = 1 2 8 7
So the total of solutions we are searching for, is: 3 3 6 4 9 − 6 × 1 2 8 7 = 3 3 6 4 9 − 7 7 2 2 = 2 5 9 2 7 .