Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Toothpicks by mortonfox
# From https://oeis.org/A139250
def msb(n):
t = 0
while n>>t > 0: t += 1
return 2**(t-1)
def toothpicks(n: int) -> int:
k = (2 * msb(n)**2 + 1) / 3
return 0 if n == 0 else k if n == msb(n) else k + 2*toothpicks(n - msb(n)) + toothpicks(n - msb(n) + 1) - 1
print("Example:")
print(toothpicks(2))
# These "asserts" are used for self-checking
assert toothpicks(1) == 1
assert toothpicks(2) == 3
assert toothpicks(3) == 7
assert toothpicks(4) == 11
assert toothpicks(5) == 15
print("The mission is done! Click 'Check Solution' to earn rewards!")
March 9, 2023