Project Euler 154 - Exploring Pascal's Pyramid

Official link: https://projecteuler.net/problem=154

Note: Code takes ~650 seconds to run, but with pypy it only took 20 seconds

Thought Process

This was a bit of a disappointing problem in my opinion. Even with optimized code it feels impossible to reach the 1 minute benchmark in python. Once I solved the problem I could see that my solution was just as good as many other solutions in the thread.

Anyways, back to how I solved it. The first obvious step is to investigate the Wikipedia page Pascal's Pyramid there we get the useful identity that the co-efficient of x^i * y^j * z^k, which we denote with C(i, j, k) = n! / (i! j! k!) is equal to C(i, j) * C(j, k) where C(i, j) denotes the co-efficient of x^i * y^j in the binomial expansion, and we can also notice some symmetries in Pascal's triangle, which greatly speedup our algorithm:

Now this is what I believe is the key step. Once you realise that you only care about 10^12 = 2^12 * 5^12 it is obvious that you only need to keep track of the amount of 2's and 5's which divide any co-efficient. 

Now the algorithm is simple:

It is simple enough to adjust this algorithm for any power N of (x + y + z) and adjust the multiple to be of the form a^ae * b^be which is what Stephan Brumme did, and (without looking at what he did), I used his page to have more test cases which I always find extremely useful while debugging! (I had a few error bounds on i, j for my first attempt)

Interactive Code

Input 5 integers each separated by a space (N, a, ae, b, be)

Code will output number of coefficients in the expansion of (x + y + z)^N are multiples of a^ae * b^be