Project Euler #114: Counting block combinations I

  • + 1 comment

    Did not expect the major obstacle to be OverflowError from exponentiation of (sqrt(5)+1)/2. Horrible use of diagonalization without numpy.

    EDIT: Learned the Decimal module, which helps in calculating very large decimal value. But got stuck with insufficient decimal.getcontext().prec as the value after exponentiation is very, very large (thousands? millions? or even more of digits), while I want every single digit to the left of the decimal point. I wonder if anything could be done to this intermediate step, as there is still crucial ahead before performing the final modulo opeartion. And the I obtained is also subject to significant error as it is found with np.linalg.eig(). Heck, there is even decimal.Overflow: above Emax!

    EDIT 2: When I looked closely, unfortunately, this approach seems to start deviating from the results of the DP approach (which is slower but correct) for (763701899 vs 763701898). Using numpy without Decimal is even worse. When the input is 2000 3 then it is the embarrassing 973324784 vs nan vs 709134169.

    EDIT 3: Stupid me to have thought about diagonalization. It's not the only option to achieve matrix exponentiation. Simple recursive divide and conquer like mergesort can do the job. And one more thing, construct your matrix according to .

    Now perhaps I'm ready for Problem 109 Darts.