Little Ashish's Huge Donation

  • + 0 comments

    Given the sum of squares formula, the number of days to spend x candies is roughly the cube root of 3x. So I back off a few days from that estimate to be conservative and then iterate through until I find the right value.

    def solve(candies):
        day = math.floor(pow(3*candies,1/3))-3
        while day*(day+1)*(2*day+1) <= 6*candies:
            day += 1
        return day-1