Project Euler #112: Bouncy numbers

  • + 1 comment

    Sweet, finally got it! I'll offer some guidance to those who are stuck (without spoilers, of course), because unfortunately this appears to be one of those problems with binary scoring, so it can be difficult to judge whether or not you're on the right track.

    • It is important to be able to efficiently calculate the count of bouncy numbers for any number containing between 3 and ~30 digits. I used iterative digit DP to accomplish this. (https://www.geeksforgeeks.org/digit-dp-introduction/)

    • When performing the search for the solution, you should increase the value of the result according to the distance between your current ratio of bouncy numbers and n/m. There is some pretty straightforward algebra that can be used to approximate the value you should increase your result by (which has been discussed in this problem's forum on the Project Euler website), but this method will overshoot the correct answer as the size of the numbers increases, so you'll need an additional step to minimize your result.

    • Initially I was trying to find the solution using a rather complex DFS function, loaded with branching conditionals. While this approach did produce the correct results, it was much too slow. My final solution did not require recursion or any sort of tree traversal.

    • As others have mentioned, some of the results will require more than 64 bits. I used Python for this reason.

    ========================================

    Some large test cases, with solutions:

    (999999999999996604 1000000000000000000) = 27115965547703180212015
    (999999999999998764 1000000000000000000) = 75283765372168284789645
    (999999999999997593 1000000000000000000) = 38415085583714167012880
    (999999999999998242 1000000000000000000) = 52651358930602957906713
    (999999999999991948 1000000000000000000) = 8205762543467461500249
    (999999999999999241 1000000000000000000) = 169299150197628458498024
    (999999999999991578 1000000000000000000) = 7739998337687010211352
    (999999999999991565 1000000000000000000) = 7727886899822169531714
    (999999999999999387 1000000000000000000) = 209622115823817292006526
    (999999999999997840 1000000000000000000) = 42808060185185185185186