Skip to content

Instantly share code, notes, and snippets.

@orlp
Created May 13, 2023 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orlp/26efd8aa77a48ad32c884d9978e7bf24 to your computer and use it in GitHub Desktop.
Save orlp/26efd8aa77a48ad32c884d9978e7bf24 to your computer and use it in GitHub Desktop.
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