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
def calculate_2048(size): highest = 2 ** (size**2 + 1) sumtotal = (2 ** (size**2 + 2))-4 return highest, sumtotal
- def calculate_2048(size):
sq = 4for _ in range(0,size*size-1): #calculating the highest square by simply multiplying it by 2sq *= 2highest = sqsumtotal = sqfor _ in range(0,size*size-1): #calculating the highest score by adding up every number back down to 4sq = 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