Ad

An optimised algorithm that produces the same result as the original, getting the highest possible value of a piece on a 2048 board, and the highest summed value of all the pieces on the board

Code
Diff
  • def calculate_2048(size):
        highest = 2 ** (size**2 + 1)
        sumtotal = (2 ** (size**2 + 2))-4
        return highest, sumtotal
    • def calculate_2048(size):
    • sq = 4
    • for _ in range(0,size*size-1): #calculating the highest square by simply multiplying it by 2
    • sq *= 2
    • highest = sq
    • sumtotal = sq
    • for _ in range(0,size*size-1): #calculating the highest score by adding up every number back down to 4
    • sq = int(sq/2)
    • sumtotal = int(sumtotal+sq)
    • return highest, int(sumtotal)
    • highest = 2 ** (size**2 + 1)
    • sumtotal = (2 ** (size**2 + 2))-4
    • return highest, sumtotal