Skip to content

Instantly share code, notes, and snippets.

@orlp
Created May 13, 2023 22:23
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
import math
for n in range(1, 65):
valid = [
(l, r)
for l in range(10 + math.ceil(math.log2(n + 1)))
for r in range(10 + math.ceil(math.log2(n + 1)))
if 2**l - 1 <= n and 2**r - 1 <= n and 2**l + 2**r - 1 >= n
]
cost = lambda l, r: 2**l * l + (n + 1 - 2**l) * r
minimal = min(cost(l, r) for l, r in valid)
for l, r in valid:
if cost(l, r) == minimal:
print(f"{n:2} {l:2} {r:2}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment