I am onion!

Cody has 4 types of onions:

  • The number of purple \color{#69047E}\text{purple} onions can be any non-negative integer.
  • The number of green \color{#20A900}\text{green} onions is a multiple of 2.
  • The number of red \color{#D61F06}\text{red} onions is a multiple of 3.
  • The number of blue \color{#3D99F6}\text{blue} onions is a multiple of 5.

If Cody has 23 onions, how many different distributions of colors can there be?


The answer is 127.

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.

7 solutions

Aditya Raut
Jul 29, 2014

We can use generating functions!

We want to find the number of non-negative integer solutions to a + b + c + d = 23 a+b+c+d=23 a = k a=k where 0 k 23 0\leq k\leq 23 ,

b = 2 k b=2k where 0 k 11 0\leq k \leq 11 ,

c = 3 k c=3k where 0 k 7 0\leq k \leq 7 and

d = 5 k d=5k where 0 k 4 0\leq k \leq 4 . (No one can exceed 23)

So the generating function will be ( k = 0 23 x k ) ( k = 0 11 x 2 k ) ( k = 0 7 x 3 k ) ( k = 0 4 x 5 k ) \displaystyle \biggl(\sum_{k=0}^{23} x^k \biggr) \biggl(\sum_{k=0}^{11} x^{2k} \biggr) \biggl(\sum_{k=0}^7 x^{3k} \biggr) \biggl(\sum_{k=0}^4 x^{5k} \biggr)

In this, use the formulas of Polynomial expansions or easier way, use Wolfram input

And you see that the co-efficient of x 23 x^{23} in the expansion is 127 \boxed{127} .

@Aditya Raut I think there was a tiny bit of sarcasm in 'use the formulas of Polynomial expansions', wasn't there? :P - Saying this because you would have to find the coefficient of x 23 x^{23} in the Taylor expansion of ( ( 1 x ) ( 1 x 2 ) ( 1 x 3 ) ( 1 x 5 ) ) 1 ((1-x)(1-x^2)(1-x^3)(1-x^5))^{-1} , which is, I think, humanly impossible to do..

Pratik Shastri - 6 years, 7 months ago

Log in to reply

I agree, but it's not impossible. It's "DIfficult" . But if you use right formula that is a worth 4 minutes thing.

Aditya Raut - 6 years, 7 months ago

Log in to reply

What do you mean by the right formula? Can you write out your process in obtaining 127 as the coefficient? I have reached the step where I have the generating functions as a product of 4 polynominals. If I expand by hand, it would be the same as counting the individual cases that satisfy a + b + c + d = 23 under the given conditions. Thanks!

Messy Tiger - 6 years, 3 months ago

It's actually humanly impossible..as this would be the same thing as finding answers by hit and trial.

Vishal Yadav - 4 years, 2 months ago

Had reached that equation of generating function but didn't know how to find the co-efficient. Can you give its detailed version(rather than wolfram version)?

Kartik Sharma - 6 years, 5 months ago

Log in to reply

Lol, it took me 10min to calculate ((1-x)(1-x^2))^-1 and i got (sum from 0 to inf of:0,25 (2n+(-1)^(n+2) x^n.Further it's too hurd to calculate.I think there must be a simple way to calculate this monster,but i don't understand why nobody could do this.

Липский Lipskiy - 1 year, 10 months ago

Mathematica Code:

1
2
3
Coefficient[
 Sum[x^i, {i, 0, 25}] Sum[x^(2 i), {i, 0, 25}] Sum[
   x^(3 i), {i, 0, 25}] Sum[x^(5 i), {i, 0, 25}], x, 23]

Agnishom Chattopadhyay - 6 years, 4 months ago

How do we solve it by hand only?

Sahil Jain - 4 years, 3 months ago

I've never heard of generating functions!

Cody Johnson - 6 years, 10 months ago

Log in to reply

Yeah, you have never heard of it because you always "typed of it" , and "used it" ..... So you got no time to hear it :( @Cody Johnson

Aditya Raut - 6 years, 10 months ago

Log in to reply

I read this topic under the name of multinomial theorem in my schools....I didn't know that this is called generating functions..... Thnx for your useful info. By the way , are you preparing for JEE 2015 ?? and 1 more thing , I had used this method of 'finding the coefficient' but never understood the logic behind it.. can u please explain it to me ?

Vighnesh Raut - 6 years, 5 months ago

Log in to reply

@Vighnesh Raut Read the brilliant wiki on generating functions, it will help a lot. Or better yet, watch some YouTube videos.

Govind Choudhary - 2 years, 10 months ago
Daniel Friedman
May 29, 2017

The dynamic programming approach is slower (and bigger) than the computation given through generating functions.

1 1 x a = 1 + x a + x 2 a + \frac{1}{1-x^a} = 1+x^a + x^{2a} +\dotsb is equivalent to using multiples-of- a a type onions. So the number of ways we can make n n using Purple, Green, Red, and Blue is given by the n n th coefficient of

1 1 x 1 1 x 2 1 1 x 3 1 1 x 5 = n = 0 a n x n \frac{1}{1-x}\cdot\frac{1}{1-x^2}\cdot\frac{1}{1-x^3}\cdot\frac{1}{1-x^5} = \sum_{n=0}^{\infty} a_nx^n and given that ( 1 x ) ( 1 x 2 ) ( 1 x 3 ) ( 1 x 5 ) = 1 x x 2 + x 4 + x 7 x 9 x 10 + x 11 , (1-x)(1-x^2)(1-x^3)(1-x^5) = 1-x-x^2+x^4+x^7-x^9-x^{10}+x^{11}, we obtain 1 = ( 1 x x 2 + x 4 + x 7 x 9 x 10 + x 11 ) n = 0 a n x n . 1 = (1-x-x^2+x^4+x^7-x^9-x^{10}+x^{11}) \sum_{n=0}^{\infty} a_nx^n. = n = 0 a n x n n = 1 a n 1 x n n = 2 a n 2 x n + n = 4 a n 4 x n + n = 7 a n 7 x n n = 9 a n 9 x n n = 10 a n 10 x n + n = 11 a n 11 x n = \sum_{n=0}^{\infty} a_nx^n - \sum_{n=1}^{\infty} a_{n-1}x^n - \sum_{n=2}^{\infty} a_{n-2}x^n + \sum_{n=4}^{\infty} a_{n-4}x^n + \sum_{n=7}^{\infty} a_{n-7}x^n - \sum_{n=9}^{\infty} a_{n-9}x^n - \sum_{n=10}^{\infty} a_{n-10}x^n + \sum_{n=11}^{\infty} a_{n-11}x^n

If we define a n = 0 a_n = 0 for all n < 0 n<0 , then we may rewrite this as 1 = n = 0 ( a n a n 1 a n 2 + a n 4 + a n 7 a n 9 a n 10 + a n 11 ) x n 1 = \sum_{n=0}^{\infty} \left(a_n-a_{n-1}-a_{n-2}+a_{n-4}+a_{n-7}-a_{n-9}-a_{n-10}+a_{n-11}\right) x^n

By equating coefficients then we obtain the recurrence relation that for n > 0 n>0 , a n = 0 , a 0 = 1 a_{-n}=0,\;a_0=1 and

a n = a n 1 + a n 2 a n 4 a n 7 + a n 9 + a n 10 a n 11 a_n = a_{n-1}+a_{n-2}-a_{n-4}-a_{n-7}+a_{n-9}+a_{n-10}-a_{n-11}

This is easy enough to compute to a 27 a_{27} by hand. But for those of us too lazy for such tasks, a simple C implementation:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
int numberConfig(const int count)
{
  int* arr = malloc((count+1)*sizeof(*arr));
  arr[0] = 1;
  int n;
  for(n=1; n<=count; n++)
  {
    arr[n] = arr[n-1]
           + ((n-2 >= 0) ? arr[n-2] : 0)
           - ((n-4 >= 0) ? arr[n-4] : 0)
           - ((n-7 >= 0) ? arr[n-7] : 0)
           + ((n-9 >= 0) ? arr[n-9] : 0)
           + ((n-10 >= 0) ? arr[n-10] : 0)
           - ((n-11 >= 0) ? arr[n-11] : 0);
  }
  return arr[count];
}

Shreyash S
Aug 2, 2014

I used this python code to solve.

count = 0
for blue in xrange(0,24,5):
    for red in xrange(0,24,3):
        for green in xrange(0,24,2):
            for purple in xrange(24):
                if purple+green+red+blue == 23:
                    count=count+1 
print count

Very good, I appreciate everyone who can program! I also would have used the same method if I wasn't the person who posted this problem :P A good way of

Bashing

Aditya Raut - 6 years, 9 months ago
Alex Wang
Jan 7, 2015

Another way is to do casework on d (which isn't as bad as it sounds since there are only 5 cases).

Case 1: d=20.

We see that (0, 0, 3, 20) works.

(1, 2, 0, 20) and (3, 0, 0, 20) works as well.

We have 3 for this case.

Case 2: d=15 For simplicity, I'm going to leave d=15 out of the quadruple.

(0, 2, 6) and (2, 0, 6) 2 in this subcase where c=6.

(1, 4, 3), (3, 2, 3), (5, 0, 3) 3 in this subcase.

(0, 8, 0) .. (8, 0, 0) 5 in this subcase.

Total in this case is 10.

Case 3: d=10

c=12. 1 here

c=9. 3 here

c=6. 4 here

c=3. 6 here

c=0. 7 here

At this point, you should see the pattern (skip 1, skip 2). We can prove this by taking two cases (one where c is odd and where c is even).

There are 21 total here.

Case 4: d=5

c=18. 1 here

c=15. 2 here

From here on, we can just get that the total for this case is 1+2+4+5+7+8+10=37 (using our pattern).

Case 5: d=0

c=21. 2 here

c=18. 3 here

2+3+5+6+8+9+11+12=56 in this case.

Summing it all up, we get 3 + 10 + 21 + 37 + 56 = 127 3+10+21+37+56=\boxed{127}

Very bashy but can be done all on paper.

It is good answer but during examination there may be a chance of missing any possibility

ram rocks - 2 years, 8 months ago
Andrew Macarthur
May 3, 2020

As with almost everyone else it seems, I've solved this via generating functions and then done some computer magic to extract the required coefficient. But I thought generating functions were meant to make things easier? In the absence of technology, the calculation required to find the coefficient of x 23 x^{23} seems to me to be exactly as hard as finding the number of solutions to the original problem...

Izan Bf
Oct 18, 2018

Easy and fast O(n^2) solution implementing generating functions in python with sympy:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from sympy import poly
from sympy.abc import x

R = poly(1, x)
P = [poly(0, x)] * 4
M = [1, 2, 3, 5]
N = 23

for i in range(4):
    for k in range(0, N//M[i]+1):
        P[i] += poly(x**(M[i]*k), x)
    R *= P[i]

print("Answer:", R.coeffs()[N]) 

Vinod Kumar
Aug 8, 2018

Used Taylor series expansion at x=0 of (1/{(1-x)(1-x^2)(1-x^3)(1-x^5)} and found the coefficient of x^23, using WolframAlpha, giving answer=127.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...