import math


def select_ghostdag_k(x, delta):
	"""
	Selects the k parameter of the GHOSTDAG protocol such that anticones lager than k will be created
	with probability less than 'delta' (follows eq. 1 from section 4.2 of the PHANTOM paper)

	:param x: Expected to be 2Dλ where D is the maximal network delay and λ is the block mining rate
	:param delta: An upper bound for the probability of anticones larger than k
	:return: The minimal k such that the above conditions hold
	"""
	k_hat, sigma, fraction, exp = 0, 0, 1, math.e ** (-x)
	while True:
		sigma += exp * fraction
		if 1 - sigma < delta:
			return k_hat
		k_hat += 1
		fraction = fraction * (x / k_hat)