# Generating Functions

This week, we have a guest post written by Alexander B. on generating functions, which are useful tools for solving advanced counting problems.

How would you use generating functions to solve the following?

>

1. How many non-negative integer solutions are there to $a + b + 2c = n$ for any positive integer $n$?

2. Share a question which can be approached using Generating Functions. Note by Calvin Lin
6 years, 11 months ago

This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.

When posting on Brilliant:

• Use the emojis to react to an explanation, whether you're congratulating a job well done , or just really confused .
• Ask specific questions about the challenge or the steps in somebody's explanation. Well-posed questions can add a lot to the discussion, but posting "I don't understand!" doesn't help anyone.
• Try to contribute something new to the discussion, whether it is an extension, generalization or other idea related to the challenge.

MarkdownAppears as
*italics* or _italics_ italics
**bold** or __bold__ bold
- bulleted- list
• bulleted
• list
1. numbered2. list
1. numbered
2. list
Note: you must add a full line of space before and after lists for them to show up correctly
paragraph 1paragraph 2

paragraph 1

paragraph 2

[example link](https://brilliant.org)example link
> This is a quote
This is a quote
    # I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
# I indented these lines
# 4 spaces, and now they show
# up as a code block.

print "hello world"
MathAppears as
Remember to wrap math in $$ ... $$ or $ ... $ to ensure proper formatting.
2 \times 3 $2 \times 3$
2^{34} $2^{34}$
a_{i-1} $a_{i-1}$
\frac{2}{3} $\frac{2}{3}$
\sqrt{2} $\sqrt{2}$
\sum_{i=1}^3 $\sum_{i=1}^3$
\sin \theta $\sin \theta$
\boxed{123} $\boxed{123}$

Sort by:

The number of solutions, $c_n$, is the coefficient of $x^n$ in the expansion of $\frac{1}{(1-x)^2(1-x^2)} \; = \; \frac{1}{(1-x)^3(1+x)} \; = \; \left(\sum_{j=0}^\infty {j+2 \choose 2}x^j\right)\left(\sum_{k=0}^\infty (-1)^kx^k\right)$ and so $\begin{array}{rcl} c_n & = & \sum_{m=0}^n (-1)^{n-m}{m+2 \choose 2} \; = \; \tfrac12(-1)^n\sum_{m=0}^n(-1)^m(m+1)(m+2) \\ & = & \tfrac14(-1)^n\sum_{m=0}^n (-1)^m\big[(m+1)^2+(m+2)^2-1\big] \\ & = & \tfrac14(-1)^n\left[\sum_{m=0}^n (-1)^m(m+1)^2 - \sum_{m=1}^{n+1}(-1)^m(m+1)^2 - \sum_{m=0}^n(-1)^m\right] \\ & = &\tfrac14(-1)^n\left[1 + (-1)^n(n+2)^2 - \sum_{m=0}^n(-1)^m\right] \; = \; \tfrac14\big[(n+2)^2 - \varepsilon_n\big] \end{array}$ where $\varepsilon_n \; = \; \left\{ \begin{array}{lcl} 0 & \qquad & n \mbox{ even} \\ 1 & \qquad & n \mbox{ odd} \end{array} \right.$ and hence $c_n \; = \; \left \lfloor \tfrac14(n+2)^2 \right\rfloor$

- 6 years, 11 months ago

Can you use generating functions when you are picking 2 or more distinct elements from one set?

- 6 years, 11 months ago

Consider $f(x)^2 - f(x^2)$.

What would it look like for picking 3 distinct objects?

Staff - 6 years, 11 months ago

If $f(x) = x^4 + x^5 + x^6 + x^7$ then it would be $(f(x))^3 - x^4 f(x^2) - x^5 f(x^2) - x^6f(x^2) - x^7f(x^2)$?

- 6 years, 11 months ago

Write it out and check :)

Your formula looks like $f(x)^3 - f(x) f(x^2)$.
If it works, why does it work?
If it doesn't work, why doesn't it work?

Staff - 6 years, 11 months ago

Ah, brilliant. Though as you start picking 3, 4, 5, etc. elements this doesn't become very practical anymore.

To your edit-- It would be $f(x)^3 - f(x^3) -$ some other things to get rid of the products in which only a pair are the same. I'll think about it.

- 6 years, 11 months ago

There is a 'nice' generalized formula. Think about how I created the function, and why it works.

I don't think your example works.

Hint: Just because you learnt an advanced technique, doesn't mean that you should forget your basics.

Staff - 6 years, 11 months ago

I know mine doesn't work, my sentence comes out weird in latex but what I meant was "this minus some other things." Your first one works because you take the product and then subtract the case when two of the same element is being multiplied by each other, but when you scale up to three you have to worry about all three of them being the same as well as them being not pairwise distinct (e.g. $x^4 \times x^4 \times x^7$). However, while a term being multiplied three times occurs only once, the situation where two terms plus another distinct term occurs thrice. This makes me come to the formula of $f(x)^3 - 3f(x)f(x^2) + 2f(x^3)$. It's multiplying by three, then taking out three times the pairwise non-distinct as well as the triple non-distinct, and then adding two of the triple non-distincts back in since that only occurs once in the original expansion of $f(x)^3$. Though I'm a bit doubtful that this one works either..

- 6 years, 11 months ago

To elaborate on my hint, use the Principle of Inclusion and Exclusion.

This tells you why the formula looks the way it does (and also why it gets ugly quickly). Interestingly, it only involves terms of the form $f(x^n)$, so if you have a simple description of $f(x)$, then you might be able to get a simple description for the generating function of distinct elements.

Staff - 6 years, 11 months ago

Awesome..Till now, I was completely unknown to this concept,(I had seen it in problems but never understood )but now I am very interested in it...I am very happy now..thnx Alexander and Calvin.

- 6 years, 11 months ago

After you finish number 1, try this:

How many non-negative integer solutions are there to $a+b+2c=n$ for any positive integer $n$ for distinct $a, b, c$?

- 6 years, 11 months ago

If $d_n$ is the number of solutions with $a=b$, then $d_n \; = \; \left\{ \begin{array}{lcl} \tfrac12n+1 & \quad & n \mbox{ even} \\ 0 & \quad & n \mbox{ odd} \end{array}\right.$ If $e_n$ is the number of solutions with $a=c$ or $b=c$ then $e_n \; = \; 1 + \big\lfloor \tfrac13n\big\rfloor$ If $f_n$ is the number of solutions with $a=b=c$ then $f_n \; = \; \left\{ \begin{array}{lcl} 1 & \quad & 4\,|\,n \\ 0 & \quad & 4 \not| n \end{array} \right.$ and the number of solutions with $a,b,c$ distinct is $c_n - d_n - 2e_n + 2f_n$ where $c_n = \big\lfloor \tfrac14(n+2)^2\big\rfloor$ is as in my other post.

- 6 years, 11 months ago