What's the number?

How many integers between 1 and 1,000,000 have the sum of their digits equal to 18?


The answer is 25927.

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.

6 solutions

Dieuler Oliveira
Aug 9, 2014

We can think of the solutions of this problem as numbers with 6 6 digits, since they are between 1 1 and 1 , 000 , 000. 1,000,000. So we are searching the solutions from the following equation: x + y + z + t + w + u = 18. x+y+z+t+w+u=18. The total of solutions for this equation is: ( 23 18 ) = 33649. {{23}\choose{18}}=33649.

An example for this solution is:

( 0 , 0 , 1 , 3 , 5 , 9 ) (0,0,1,3,5,9) , which is equivalent to the number: 1359 1359 .

Now we have to take out the solutions which contain digits from 10 10 to 18 18 . We have to choose one of the variables to be one of these numbers above, so we have 6 6 options to set each of them. Now we have to multiply by 6 6 the total of solutions from the following inequality: x + y + z + t + w 8. x+y+z+t+w \leq 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. x+y+z+t+w+u=8. Notice that, by introducing an extra variable, you insert solutions from 0 0 to 8 8 , which is equivalent to the inequality above except by one variable.

The total of solutions from this equation is: ( 13 8 ) = 1287 {{13}\choose{8}}=1287

So the total of solutions we are searching for, is: 33649 6 × 1287 = 33649 7722 = 25927 . 33649-6\times1287=33649-7722=\boxed{25927}.

Why you said that the total number of solutions for the first equation is 23C18 ?

ahmed hussein - 6 years, 7 months ago

Log in to reply

http://en.wikipedia.org/wiki/Stars and bars_(combinatorics)

Dieuler Oliveira - 6 years, 7 months ago
Krit Phuengphan
Jun 1, 2014

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 0 to 9 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 f(x)=(1+x+x^2+...+x^8+x^9)^6 That is, f ( x ) = ( 1 x 10 1 x ) 6 f(x)=(\frac{1-x^{10}}{1-x})^6 which can be explicitly stated as f ( x ) = ( 1 + r = 1 6 ( 1 ) r C 6 , r x 10 r ) ( 1 + r = 1 C r + 6 1 , r x r ) f(x)=(1+\sum^{6}_{r=1}(-1)^rC_{6,r}x^{10r})(1+\sum_{r=1}^{\infty}C_{r+6-1,r}x^r) We eventually get the answer, i.e. the coefficient of 18-powered x x term, equal to C 23 , 18 6 C 13 , 8 = 25927 C_{23,18}-6*C_{13,8}=25927

ps. I failed with my wrong calculating skill in first two tries. LOL

Krit Phuengphan - 7 years ago

Log in to reply

can u give a link for more information on Generating Function??

Harshvardhan Mehta - 6 years, 11 months ago

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!

Dieuler Oliveira - 6 years, 10 months ago
Brock Brown
Dec 27, 2014

Lazy Pythonic approach:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
def digit_sum(x):
    total = 0
    for digit in str(x):
        total += int(digit)
    return total
a = 1
count = 0
while a <= 1000000:
    if digit_sum(a) == 18:
        count += 1
    a += 1
print count

Vishal Sharma
Dec 7, 2014

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
def sumDigits( n ):
    ans = 0
    while n:
        ans += n%10
        n /= 10
    return ans    

ctr = 0
for i in range( 99, 10**6 + 1 ):
    if sumDigits(i)==18:
        ctr += 1
print ctr

Rajat De
Dec 5, 2014

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; }

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...