The problem requires to generate the n-th element of a sequence that is similar to Fibonacci sequence. However, it\'s a bit tricky because n is ver
Just thinking here but take a look at Duff's device for the count_fair_coins function as that will automatically unroll the loop to speed up that function.
Precomputing the sqrt's in the generating function seems like the easiest way to get any speed up. Which would reduce to just a single pow call and multiplication of constants. As well as precomuting the sqrt's another way to speed it up is to remove the divisions and use the inverse multiplication, although a very slight optimization it might help speed up when n is very large.