Example #1
0
 def choose_action(self) -> Action:
     """choose action at random"""
     self.correct_invalid()
     n = choose(self.get_allowed_values())
     return Action(self.get_allowed_coo()[n])
Example #2
0
# Find the sum of the distinct squarefree numbers in the first 51 rows of
# Pascal's triangle

from useful import PrimeList, choose

primes = PrimeList(10**6)

def squarefree(n) :
  for prime in primes :
    if prime > n :
      return True
    if n % prime == 0 :
      if (n / prime) % prime == 0 :
        return False
      n = n / prime
  return True

se = set([])

row_up_to = 51
for j in xrange(row_up_to) :
  for i in xrange(j/2 + 1) :
    num = choose(j, i)
    if (squarefree(num)) :
      se.add(num)

print sum(se)
Example #3
0
from useful import PrimeSet, choose, fact

numDiscs = 100
outOfOrder = 22

primes = PrimeSet(numDiscs)
numPrimes = len(primes)
numComps = numDiscs - numPrimes

def recurse(num,total) :
	if num == 0 :
		return (0,1)
	else :
		val = recurse(num-1,total)
		first = (num-1) * val[0]
		second = (val[1] - first)
		return (second, first*(total-num+1) + second*(total-num))

def ways(num,total) :
	return recurse(num,total)[1]

top = choose(numPrimes,outOfOrder) * ways(outOfOrder,numDiscs-numPrimes+outOfOrder) * fact(numComps)
bottom = fact(numDiscs)

print float(top)/bottom
Example #4
0
  x = [0] * bitlen
  count = 0
  while x != [1]*bitlen :
    for i in xrange(len(x)) :
      if random.randint(0,1) == 1 :
        x[i] = 1
    count += 1
  n += count

print n / 1000.0
exit(1)
"""

ev = 0
i = 1
num1s = [0] * (bitlen + 1)
num1s[0] = 1
while True:
    lastNum1s = num1s
    num1s = [0] * (bitlen + 1)
    for x in xrange(len(num1s)):
        for j in xrange(x + 1):
            n = bitlen - j
            num1s[x] += lastNum1s[j] * choose(n, x - j) * (1.0 / 2 ** (n))
    curr = i * (num1s[bitlen] - lastNum1s[bitlen])
    i += 1
    if ev + curr == ev:
        break
    ev += curr
    print ev, curr