Skip to content

Instantly share code, notes, and snippets.

@dmd
Created December 3, 2023 15:59
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 dmd/6e0ce9edec8872e55ad9e877c5b87489 to your computer and use it in GitHub Desktop.
Save dmd/6e0ce9edec8872e55ad9e877c5b87489 to your computer and use it in GitHub Desktop.
from math import comb
def probability_of_eights(sequence_length, total_string_length=64):
digits = 16
def p(k):
placements = comb(total_string_length - k * (sequence_length - 1), k)
combinations = digits ** (total_string_length - k * sequence_length)
total_ways = digits ** total_string_length
return placements * combinations / total_ways
probability = 0
sign = 1
for k in range(1, total_string_length // sequence_length + 1):
probability += sign * p(k)
sign *= -1
return probability
sequence_length = int(sys.argv[1])
print(probability_of_eights(sequence_length))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment