예제 #1
0
파일: p66.py 프로젝트: fazz/soft
def convergent(i, n):
	base = int(sqrt(i))

	c = [base]

	diff = -base
	numerator = 1
	for z in range(1, n):
		d = i - diff**2
		if not gcd(numerator, d) == numerator:
			raise "ERROR"
		d /= numerator
		numeratordiff = -diff
		x = 0
		while abs(numeratordiff-d) <= base:
			numeratordiff -= d
			x += 1
		c.append(x)
		diff = numeratordiff
		numerator = d

	# compute output
	a = c.pop()
	d = 1
	while len(c):
		b = a
		a = a*c.pop() + d
		d = b

	return (a, d)
예제 #2
0
파일: p269.py 프로젝트: fazz/soft
def Z(k):
	r = 0
	for i in range(10, k+1):
		if i%10 == 0:
			r += 1
		else:
			t = i/10**(len(str(i))-1)
			if gcd(i%10, t) == t:
				r += 1
	return r
예제 #3
0
    def generate_keys(self):
        print('Generating keys for RSA')
        _bits = 8
        p, q = randPrime(_bits), randPrime(_bits)
        while (p == q):
            p = randPrime(3)
            q = randPrime(3)
        # print ('p =',p,', q =',q)
        n = p * q
        phi = (p - 1) * (q - 1)
        # an e that makes E and PHI coprime
        e = random.randrange(3, phi - 1)
        # verify coprime-ness
        g = gcd(e, phi)
        while g != 1:
            e = random.randrange(3, phi - 1)
            g = gcd(e, phi)

        d = modInv(e, phi)
        # self.public_key = (n, e)
        # self.private_key = (n, d)
        return ((n, e), (n, d))  #public, private
예제 #4
0
# 30872nd
import tools


def multiplicative_order(a, n):
    base = a
    for i in range(1, n):
        if a % n == 1:
            return i
        a *= base


if __name__ == "__main__":
    max = (0, 0)

    for i in range(2, 1000):
        if tools.gcd(i, 10) == 1:
            x = multiplicative_order(10, i)
            if x > max[1]:
                max = (x, i)

    print max
예제 #5
0
파일: p75.py 프로젝트: fazz/soft
limit = 1500000

i = 12
result = 0

primitives = []

p = 0
ps = set()
ds = set()
for m in range(2, 867):
	for n in range(1, m):
		if m%2 == n%2:
			continue
		if tools.gcd(m, n) == 1:
			p += 1
			x = 2*m*(m+n)
			if x in ps:
				ds.add(x)
			ps.add(x)
			
while i <= limit:
	
	j = i/2

	if tools.isprime(j):
		i += 2
		continue

	(f, c) = tools.primefactorization(primes, j, 1000000)
예제 #6
0
파일: p71.py 프로젝트: fazz/soft
import tools


target = 8
target = 1000000

resultn = 1
resultd = target

for d in xrange(2, target+1):
	s = max(1, int(d*resultn*1.0/resultd))
#print s*1.0/d, 3.0/7, s*1.0/d <= resultn*1.0/resultd
	for n in xrange(s, int(d*3.0/7.0)+1):
		if tools.gcd(n, d)-1:
			continue
		
		if resultn*1.0/resultd < n*1.0/d and 3.0/7.0 > n*1.0/d:
			resultn = n
			resultd = d

print resultn, resultd

예제 #7
0
파일: p86.py 프로젝트: fazz/soft
def iscoprime(a, b):
	return tools.gcd(a, b) == 1
예제 #8
0
def sqrt_mod_prime_pow(x, p, n):
    if x % p:
        return hensel_lift(x, p, n)
    else:
        res = []
        pn = pow(p, n)
        y = mod(x, pn)
        # Case 1
        if y == 0:
            m, i = n >> 1, 0
            pm = pow(p, m + 1) if (n & 1) == 1 else pow(p, m)
            while i < pn:
                res.append(i)
                i += pm
            return res
        else:
            g = gcd(x, pn)
            r = 0
            # Extract factors of the prime from the gcd
            while g % p == 0:
                g /= p
                r += 1
            if (r & 1) == 1:
                return None
            m = r >> 1
            x1 = x >> r
            if p == 2:
                # Case 2a
                if n - r == 1:
                    pmn1 = 1 << (n - m + 1)
                    pm1 = 1 << (m + 1)
                    k, i = pm1 << 1, pm1 >> 1
                    while i < pmn1:
                        j = i
                        while j < pn:
                            res.append(j)
                            j += k
                        i += pm1
                # Case 2b
                elif n - r == 2:
                    res1 = hensel_lift(x1, p, n - r)
                    if res1 is None:
                        return None
                    pnm, s = 1 << (n - m), set([])
                    for r in res1:
                        i = 0
                        while i < pn:
                            x = (r << m) + i
                            s.add(x)
                            i += pnm
                    res = list(s)
                # Case 2c
                else:
                    res1 = hensel_lift(x1, p, n - r)
                    if res1 is None:
                        return None
                    pnm1, s = 1 << (n - m - 1),  set([])
                    for r in res1:
                        i = 0
                        while i < pn:
                            x = mod((r << m) + i, pn)
                            s.add(x)
                            i += pnm1
                    res = list(s)
            # case 3
            else:
                m = r >> 1
                x1 = x // pow(p, r)
                res1 = hensel_lift(x1, p, n - r)
                if res1 is None:
                    return None
                res1 = res1[0]
                pm = pow(p, m)
                pnr = pow(p, n - r)
                pnm = pow(p, n - m)
                i = 0
                while i < pnm:
                    x = mod(res1 + i, pn)
                    res.append(x * pm)
                    i += pnr
            return res               
예제 #9
0
#30872nd
import tools


def multiplicative_order(a, n):
    base = a
    for i in range(1, n):
        if a % n == 1:
            return i
        a *= base


if __name__ == "__main__":
    max = (0, 0)

    for i in range(2, 1000):
        if tools.gcd(i, 10) == 1:
            x = multiplicative_order(10, i)
            if x > max[1]:
                max = (x, i)

    print max
예제 #10
0
 def cont(self):
     return gcd(*self.coef)
def depth_position_attribute(depth, position):
    g = gcd(depth - position, position)
    return (depth - position) / g, position / g
def depth_position_attribute(depth, position):
    g = gcd(depth - position, position)
    return (depth - position) / g, position / g