diff --git a/akstest.py b/akstest.py new file mode 100644 index 0000000..9092da0 --- /dev/null +++ b/akstest.py @@ -0,0 +1,84 @@ +''' +cache = {} + +def binomcoeffb(n,k): + nom = 1 + denum = 1 + for i in range(n-k): + nom *= (n-i) + denum *= (i+1) + v = nom//denum + cache[(n,k)] = v + return v + +def binomcoeffc(n,k): + if (n,k) in cache: + return cache[(n,k)] + if n == k or k == 0: + return 1 + v = binomcoeffc(n-1,k-1) + binomcoeffc(n-1,k) + cache[(n,k)] = v + return v + +def binomcoeff(n,k): + try: + v = binomcoeffc(n,k) + except RuntimeError: + v = binomcoeffb(n,k) + return v + +def binomcoeffi(): + l = [1,2,1] + i = 3 + yield (i,l[1:-1]) + + while True: + i += 1 + nl = [1] + for j in range(len(l)-1): + nl.append( l[j]+l[j+1] ) + nl.append(1) + l = nl + yield (i,l[1:-1]) + +def is_prime(n): + if n <= 1: + return False + for n + bc = binomcoeff(n,k) + r = bc % n +# print("%d %% %d = %d" % (bc,n,r)) + if r != 0: + return False + return True +''' +class PrimeGenerator: + def __init__(self): + self.coeffs = [1,1] + self.prime = 1 + + def nextPrime(self): + self.step() + while not self.check(): + self.step() + return self.prime + + def step(self): + l = [1] + for j in range(len(self.coeffs)-1): + l.append(self.coeffs[j] + self.coeffs[j+1]) + l.append(1) + self.coeffs = l + self.prime += 1 + + def check(self): + for i in range(1,len(self.coeffs)//2+1): + if self.coeffs[i] % self.prime != 0: + return False + return True + + +pg = PrimeGenerator() + +for i in range(10001): + print("%d: %d" % (i,pg.nextPrime()) ) diff --git a/back2black.py b/back2black.py new file mode 100644 index 0000000..611b781 --- /dev/null +++ b/back2black.py @@ -0,0 +1,20 @@ +from sys import stdout as out + +p = {} +for i in range(1,91): + p[i] = 0 + +for i in range(2,91): + n = i + while n <= 90: + p[n]+=1 + n += i + +for i in range(9): + for j in range(10): + if p[i*10+j+1] % 2 == 0: + out.write("o") + else: + out.write(" ") + out.write("\n") + diff --git a/binomial.py b/binomial.py index 3ecf638..283a92c 100644 --- a/binomial.py +++ b/binomial.py @@ -1,34 +1,13 @@ -def pascal(n): - if n == 0: - return [1] - p = [1,1] - for i in range(n-1): - pn = [1] - for j in range(len(p)-1): - pn.append(p[j]+p[j+1]) - pn.append(1) - p = pn - return p +def binomcoeff(n,k): + nom = 1 + denum = 1 + for i in range(n-k): + nom *= (n-i) + denum *= (i+1) + v = nom//denum + return v -def binomial(n): - i = n - j = 0 - for c in pascal(n): - yield (c,i,j) - i -= 1 - j += 1 +def E(n,k): + return binomcoeff(2*n+k,n)*binomcoeff(n+k,n)//2 -if __name__ == "__main__": - print("Form: (x+y)^n") - x = float(input("Insert x:")) - y = float(input("Insert y:")) - n = int(input("Insert n:")) - - assert(n>0) - res = 0 - s = [] - for (c,i,j) in binomial(n): - s.append("%d*(%.2f)^%d*(%.2f)^%d" % (c,x,i,y,j)) - res += c * x**i * y**j - print(" + ".join(s)) - print("Result: %f" % res) +print(E(1,2), E(2,1), E(2,2), E(3,1)) diff --git a/check_prime.py b/check_prime.py new file mode 100644 index 0000000..1ef61be --- /dev/null +++ b/check_prime.py @@ -0,0 +1,30 @@ +from sys import argv + +def binomcoeff(n,k): + nom = 1 + denum = 1 + for i in range(n-k): + nom *= (n-i) + denum *= (i+1) + v = nom//denum + return v + +def is_prime(n): + if n <= 1: + return False + for k in range(1,n): + bc = binomcoeff(n,k) + r = bc % n +# print("%d %% %d = %d" % (bc,n,r)) + if r != 0: + return False + return True + +if len(argv) > 1: + p = int(argv[1]) + if is_prime(p): + print("%d is a prime" % p) + else: + print("%d is not a prime" % p) +else: + print("%s " % argv[0]) diff --git a/crc3.py b/crc3.py new file mode 100644 index 0000000..35e5149 --- /dev/null +++ b/crc3.py @@ -0,0 +1,29 @@ +from math import log, floor + +tobin = lambda v: v>0 and tobin(v>>1).lstrip('0')+str(v&1) or '0' + +def calcremainder(v,coeff): + n = floor(log(coeff,2)) + v = v << n + r = v + while (r>>n) > 0: + m = floor(log(r,2)) + p = coeff<<(m-n) + r = r ^ p + return v,r + +def create(b,coeff=0b1011): + v,r = calcremainder(b,coeff) + return v+r + +def check(b,coeff=0b1011): + _,r = calcremainder(b,coeff) + return r == 0 + +a = int("11010011101100",2) + +print( "V:" + tobin( a ) ) +c = create(a)+1 +print("CRC-Data: " + tobin( c ) ) + +print("Correct?: " + ( check(c) and "Yes" or "No")) diff --git a/goatdoor.py b/goatdoor.py new file mode 100644 index 0000000..c76b774 --- /dev/null +++ b/goatdoor.py @@ -0,0 +1,30 @@ +from random import shuffle, randint + +result = { "swap": 0, "not_swap": 0 } + +doors = [ 0, 0, 1] +door_indices = list(range(3)) + +for i in range(500): + shuffle(doors) + shuffle(door_indices) + door_chosen, door_opened, door_swap = door_indices + if doors[door_opened] == 1: + door_swap, door_opened = door_opened, door_swap + + if doors[door_swap] == 1: + result["swap"] += 1 + elif doors[door_chosen] == 1: + result["not_swap"] += 1 + else: + print("WTF?") + break + +p100 = result["swap"] + result["not_swap"] + +p_swap = result["swap"]*100/p100 + +p_not_swap = result["not_swap"]*100/p100 + +print("Swap door\t: %.2f%%" % p_swap) +print("Don't swap door\t: %.2f%%" % p_not_swap) diff --git a/johnsbeauty.py b/johnsbeauty.py new file mode 100644 index 0000000..72712fd --- /dev/null +++ b/johnsbeauty.py @@ -0,0 +1,26 @@ +def countchars(s): + chars = list(map(chr,range(ord('a'),ord('z')+1))) + counts = {} + for c in s.lower(): + if c not in chars: + continue + if c not in counts: + counts[c] = 1 + else: + counts[c] += 1 + + keys = [ (c,counts[c]) for c in counts ] + keys.sort(key=lambda c: -c[1]) + return keys + +def maximumbeauty(s): + clist = countchars(s) + maxbeauty = 0 + beauty = 26 + for c, count in clist: + maxbeauty += count * beauty + beauty -= 1 + return maxbeauty + +for s in ["ABbCcc","Good luck in the Facebook Hacker Cup this year!","Ignore punctuation, please :)", "Sometimes test cases are hard to make up.","So I just go consult Professor Dalves"]: + print("%s: %d" % (s, maximumbeauty(s))) diff --git a/piart.py b/piart.py new file mode 100644 index 0000000..d56329d --- /dev/null +++ b/piart.py @@ -0,0 +1,24 @@ +import svgwrite as svg +import random + +def randomcolor(): + r = random.randint(100,200) + g = random.randint(100,200) + b = random.randint(100,200) + + return "#%02X%02X%02X" % (r,g,b) + +dg = svg.Drawing('piart.svg', size=(1920,1080), profile="full") + +#Background +bg = dg.add(dg.g(id='background', fill='#2C2E2F')) +bg.add(dg.rect(insert=(0,0),size=(u'100%',u'100%'),rx=0,ry=0)) + +#circle +circle = dg.add(dg.g(id='circle')) +for x in range(1,111): + for y in range(1,62): + circle.add(dg.circle(center=(10+5+x*17,10+5+y*17),r=5,fill=randomcolor())) + +dg.save() + diff --git a/piart.svg b/piart.svg new file mode 100644 index 0000000..d937dd8 --- /dev/null +++ b/piart.svg @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/spr.py b/spr.py new file mode 100644 index 0000000..3a33281 --- /dev/null +++ b/spr.py @@ -0,0 +1,53 @@ +A = "A" +B = "B" +C = "C" +#D = "D" +E = "E" +F = "F" + +nodes = [A,B,C,E,F] + +edges = { + (A,B,3), + (A,C,4), +# (B,D,5), + (B,E,2), +# (C,D,1), +# (D,E,2), +# (D,F,4), + (E,F,6) +} + +def getadjacent(node, edges): + adjacent = set() + for (s,t,l) in edges: + if s == node: + adjacent.add( (t,l) ) + elif t == node: + adjacent.add( (s,l) ) + return adjacent + + +def spr(s,t, nodes, edges): + nodes = set(nodes) + m = { s:(0,[s])} + S = set([s]) + S2 = set(S) + while S < nodes: + for s in set(S2): + S2.remove( s ) + l,p = m[s] + for (ad,sl) in getadjacent(s, edges): + if ad not in S: + m[ad] = (l+sl,p+[ad]) + else: + ad_l,_ = m[ad] + if ad_l > l+sl: + m[ad] = (l+sl,p+[ad]) + S.add( ad ) + S2.add( ad ) + return m[t] + + +print( spr(A,F, nodes, edges) ) +