def calc_prob_at_least(v,potential_e,e,i): r = Rational() for j in range(0,2*i): r.multiply_by(e - j) r.divide_by(potential_e - j) r.multiply_by(gmpy2.comb(v - 2, i)) return r.value()
def main(): start = time.time() results = multiprocessing.Queue() updates = multiprocessing.Queue() trials = 200 max_input = int(gmpy2.exp(128)) degree = int(sys.argv[2]) num_points = int(sys.argv[1]) n = multiprocessing.cpu_count() processes = [] proc_results = [] size = int(gmpy2.comb(num_points, degree)) / n combs = itertools.combinations(numpy.linspace(1, 2, num_points), degree) for i in range(n): xs = [x for _, x in zip(range(size), combs)] proc = multiprocessing.Process(target=worker, args=(xs, results, updates, trials, max_input)) print "starting process %d" % i proc.start() processes.append(proc) for i in range(2*n): print updates.get() for i in range(n): proc_results.append(results.get()) for proc in processes: proc.join() pretty_errors(min(proc_results)) print "Time Elapsed: %.2f seconds." % (time.time() - start)
def calc_prob_at_least(v, potential_e, e, i): r = Rational() for j in range(0, 2 * i): r.multiply_by(e - j) r.divide_by(potential_e - j) r.multiply_by(gmpy2.comb(v - 2, i)) return r.value()
def card(n,k,a): result = 0 a.sort(reverse=True) x = 0 for i in range(n-1,k-2,-1): result += (a[x] * comb(i,k-1)) % mod x+=1 return result % mod
def triangleDensity(graph): outV = snap.TIntPrV() snap.GetTriadParticip(graph,outV) triangles = 0.0 for pair in outV: triangles += (pair.GetVal1()*pair.GetVal2()) triangles=triangles / 3.0 #three triangles per node is added print ("number of triangles: "+str(triangles)) return (triangles/gmpy2.comb(graph.GetNodes(),3))
def triangleDensity(graph): outV = snap.TIntPrV() snap.GetTriadParticip(graph, outV) triangles = 0.0 for pair in outV: triangles += (pair.GetVal1() * pair.GetVal2()) triangles = triangles / 3.0 #three triangles per node is added print("number of triangles: " + str(triangles)) return (triangles / gmpy2.comb(graph.GetNodes(), 3))
def modulo_combs(m, n, p): """ Uses Lucas' theorem to calculate the value of m choose n, modulo p. For this to work p must be a prime - the output is indeterminate for non-prime values of p. """ product = 1 while m > 0: m, mi = divmod(m, p) n, ni = divmod(n, p) product = (product * gmpy2.comb(mi, ni)) % p if product == 0: return 0 return product
def badTriangleDensity(graph): triangles = numpy.zeros(graph.GetMxNId()) i = 0 print("starting triangle density, printing every 100 nodes competed") for currentNode in graph.Nodes(): i = i+1 if i % 100 == 0: print(i) for neighbourIndex in range(0, currentNode.GetOutDeg()): neighbourId = currentNode.GetOutNId(neighbourIndex) neighbourNode = graph.GetNI(neighbourId) for NNI in range(0, neighbourNode.GetOutDeg()): NNID = neighbourNode.GetOutNId(NNI) for realNeighbourIndex in range(0, currentNode.GetOutDeg()): realNeighbour = currentNode.GetOutNId(realNeighbourIndex) if NNID == realNeighbour: triangles[currentNode.GetId()] += 1 print "done with triangle density" print "number of triangles: {}".format(ntriangles) return ntriangles/gmpy2.comb(graph.GetNodes(), 3)
def badTriangleDensity(graph): triangles = numpy.zeros(graph.GetMxNId()) i = 0 print("starting triangle density, printing every 100 nodes competed") for currentNode in graph.Nodes(): i = i + 1 if i % 100 == 0: print(i) for neighbourIndex in range(0, currentNode.GetOutDeg()): neighbourId = currentNode.GetOutNId(neighbourIndex) neighbourNode = graph.GetNI(neighbourId) for NNI in range(0, neighbourNode.GetOutDeg()): NNID = neighbourNode.GetOutNId(NNI) for realNeighbourIndex in range(0, currentNode.GetOutDeg()): realNeighbour = currentNode.GetOutNId(realNeighbourIndex) if NNID == realNeighbour: triangles[currentNode.GetId()] += 1 print "done with triangle density" print "number of triangles: {}".format(ntriangles) return ntriangles / gmpy2.comb(graph.GetNodes(), 3)
def height(n, m): '''height (n,m) = height(n,m-1) + height(n-1,m-1) +1 for n >0 we can demonstrate that height (n,m) = sum on i from 0 to min (m-1,n-1) factor(i,m-1) height(n-i,1) + constant with constant = 1+ sum on j from 1 to m-2 ( sum on i from 0 to min(jn-1) of factor(i,j) ) with factor(i,p) defined only with n-1>=p>=i = recursive sum on index k from 1 to p-1 ( sum on index(k) from i-k to index (k-1) of sum on index ) the last one is the sum of the index itself in total there should be i-1 sums recursively // factor (i,p) = factor(i,p-1) + factor (i-1,p-1) with factor(0,p) = 1 and factor (p,p) = 1 we need then to compute the factors f(i,p) for i from 0 to n-1 and for p from 1 to n-1 global count if count == 0: for i in range (1,5000): for j in range (i,5000): comb(i,j) count += 1 ''' if m == 0 or n == 0: constante = 0 elif m == 1: constante = 1 elif n >= m: constante = pow(2, m) - 1 else: max_index = min(n,m) ''' for i in range(1, max_index+1-2*(max_index%2), 2): print(i) constante += 2*comb(m,i) if (max_index-1)%2 ==0: constante += 2*comb(m - 1, max_index-1) #constante += 2 * comb(m - 1, i) ''' constante = 2*somme_comb(n-1, m-1) constante += gmpy2.comb(m - 1, max_index) - 1 return constante
#!/usr/bin/env python from gmpy2 import comb count = 0 for n in range(1, 101): for r in range(1, n): c = comb(n,r) if c > 1000000: count += 1 print(count)
import sys import gmpy2 tot = 0 for n in range(23, 101): for i in range(n): if gmpy2.comb(n,i) > 10**6: tot += 1 print tot
import gmpy2 total = 0 for i in range(10, 101) : for j in range(1, i) : if gmpy2.comb(i, j) >= 1000000 : total += 1 print(total)
#!/usr/bin/env python from euler import digits from gmpy2 import comb def is_asc(n): ds = digits(n) return all([a <= b for a, b in zip(ds, ds[1:])]) def is_desc(n): ds = digits(n) return all([a >= b for a, b in zip(ds, ds[1:])]) def count_asc(nums): return sum(1 for n in nums if is_asc(n)) def count_desc(nums): return sum(1 for n in nums if is_desc(n)) # (1-9)00... # 11 -> (1-9)11 = 9* # 45 -> (1-4)45 = C(1,1) + C(2, 1) + C(3, 1) + ... + C(9, 1) # 54 -> (5-9)54 = same n = 100 print(comb(n+10, n) + comb(n+9, n) - (n*10 + 2))
def bi_dist(x, n, p): return gmpy2.comb(n, x) * (p**x) * ((1 - p)**(n - x))
import copy import itertools import cPickle as pickle import gmpy2 import math # To create the dictionaries for the workers in the coded case # N is a multiple of K-choose-r with open("Dict_0.txt", "rb") as myFile: Gdict = pickle.load(myFile) K = 10 N = 12600 r = 3 kcr = int(gmpy2.comb(K, r)) g = N / kcr fid = [x for x in range(1, kcr + 1)] ltemp1 = [x for x in range(1, K + 1)] ltemp2 = itertools.combinations(ltemp1, r) WSubsetR = {} fidWSet = {} fidNodes = {} Gdist = [None] * K for j in range(K): Gdist[j] = {} i1 = 1 for x in ltemp2: WSubsetR[i1] = x fidWSet[x] = i1
from gmpy2 import comb print comb(2 * 20,20)
#!/usr/bin/env python from gmpy2 import comb print(comb(2 * 20, 20))
#!/usr/bin/env python from gmpy2 import comb print(comb(2 * 20,20))
valueDict = {} ltemp1 = [] ltemp2 = [] ltemp3 = [] ltemp4 = [] destPrank = [None] * (K) srcPrank = [None] * (K) multicastGroupMap = {} i1 = 0 i2 = 0 i3 = 0 i4 = 0 i5 = 0 i6 = 0 maxnumber = None kcr = int(gmpy2.comb(K, r)) g = N / kcr #################################### Loading sub-graph dictionary into memory ################################### with open("DictC3_%s.txt" % rank, "rb") as myFile: # change 3 to suitable r as required GdictLocal = pickle.load(myFile) myFile.close() ################################ Initializing Data ############################## Gdist = [None] * K fid = [x for x in range(1, kcr + 1)] # WSubsetR={} ltemp1 = [x for x in range(1, K + 1)]
from gmpy2 import comb # Look at Problem015.mkd to understand why the possibilities are given by # the combination of (40, 20) possibilities = comb(2 * 20, 20) print(possibilities)
#!/usr/bin/env python from gmpy2 import comb count = 0 for n in range(1, 101): for r in range(1, n): c = comb(n, r) if c > 1000000: count += 1 print(count)