def positive_solutions(f, e): """Finds all positive solutions of the polynomial function f to accuracy e using an interval solver.""" solver = KrawczykSolver(1e-8, 12) pf = PolynomialFunction(f) res_sz = pf.result_size() arg_sz = pf.argument_size() i = 0 # A counter which controls the iteration q = 0 # The current actual quadrant unit_box = Box([{0: 1}] * res_sz) solutions = [] new_solutions = solver.solve(pf, unit_box) #print j,pf,new_solutions,new_solutions solutions += new_solutions while i != __builtin__.pow(2, arg_sz) - 1: k = min_zero(i) q = q ^ __builtin__.pow(2, k) i += 1 for l in range(0, res_sz): pf[l] = flip_coefficients(pf[l], k) new_solutions = solver.solve(pf, unit_box) #print j,pf,new_solutions, for solution in new_solutions: flip_point(solution, q) #print new_solutions solutions += new_solutions return solutions
def flips(n): i = 0 j = 0 while i != __builtin__.pow(2, n) - 1: k = min_zero(i) j = j ^ __builtin__.pow(2, k) i += 1 print i, j
def try_composite(a): # print (a,d,n) # print pow(a,d,n) if __builtin__.pow(int(a), int(d), int(n)) == 1: # if (a**d)%n ==1: return False for i in range(s): if __builtin__.pow(int(a), int(2**i * d), int(n)) == n-1: return False return True # n is definitely composite
def try_composite(a): # print (a,d,n) # print pow(a,d,n) if __builtin__.pow(int(a), int(d), int(n)) == 1: # if (a**d)%n ==1: return False for i in range(s): if __builtin__.pow(int(a), int(2**i * d), int(n)) == n - 1: return False return True # n is definitely composite
def power(n,k): while True: n, k = map(int, raw_input().split()) dict1 = {} if n and k: #print ((pow(n,k) + (2 * pow(n - 1,k)) + pow(n,n) + (2 * pow(n - 1,n- 1))))% 10000007 print (pow(n,k,10000007) + (2 * pow(n - 1,k,10000007)) + pow(n,n,10000007) + (2 * pow(n - 1,n- 1,10000007))) % 10000007 #print pow(n,k,12); else: break
def is_quadratic_residue(x,n): # modular exponentiaion, as opposed to maths pow function. Otherwise we get an overflow ret=__builtin__.pow(x,(n-1)/2,n) if ret==1: return True else: return False
def prime_cycle(n): import decimal period = 1 while __builtin__.pow(10, period, n) != 1: period += 1 decimal.getcontext().prec = period return period, str(decimal.Decimal(1) / decimal.Decimal(n))
def pow(*args): try: return __builtin__.pow(*args) except TypeError: assert len(args) == 2 and isinstance(args[0], PETScVector) x = PETScVector(args[0]) PETScVector_ipow(x, args[1]) return x
def quadratic_residues(n, p): n=n%p # Factor out powers of 2 from p-1, defining Q and S as: p-1=Q2^S with Q odd q=p-1 s=0 while (q%2)==0: q/=2 s+=1 #Select a z such that the Legendre symbol(z/p)=-1 (that is, z should be a quadratic non-residue modulo p), and set c congruent to z^Q z= 2 while legendre(z, p)!=-1: z+=1 c=__builtin__.pow(z, q, p) # let r=n((q+1)/2), t congruent to n^q, m=s r=__builtin__.pow(n, (q+1)/2, p) t=__builtin__.pow(n, q, p) m=s while t!=1: # Find the lowest i, 0<i<M, such that t^(2^i) is congruent to 1, via repeated squaring t_exp=2 for i in xrange(1, m): if __builtin__.pow(t, e, p)==1: break t_exp*=2 b=__builtin__.pow(c, 2**(m - i - 1), p) r=(r*b)%p t=(t*__builtin__.pow(b,2,p))%p c=__builtin__.pow(b,2,p)%p m=i return [r, p-r]
def legendre(n, p): #when p is prime, and a is a quadratic resude mod p, the legendre symbol =1, equals -1 otherwise leg = __builtin__.pow(n, (p-1)/2, p) #same as being congurent to -1 mod p if(leg%p) == (p-1): return -1 return leg
def recurring(n): if n < 8: return 7 for d in primes_up_to_n_using_sieve(n)[::-1]: period = 1 while __builtin__.pow(10, period, d) != 1: period += 1 if d - 1 == period: break return d
def __outer_approximation(self, subdomain, depth): eps = __builtin__.pow(2.0, -depth) for (id, constraint) in self._zero.items(): if constraint(subdomain[:constraint.argument_size()]).upper( ) < 0.0 or constraint(subdomain).lower() > 0.0: return [] for (id, constraint) in self._positive.items(): if constraint( subdomain[:constraint.argument_size()]).upper() < 0.0: return [] for (id, constraint) in self._negative.items(): if constraint( subdomain[:constraint.argument_size()]).lower() > 0.0: return [] #for (id,constraint) in self._equations.items(): # constraint_range=constraint(subdomain) # if constraint_range.lower() > 0.0 or constraint_range.upper()<0.0: # return [] for (id, constraint) in self._always_negative_and_zero.items(): constraint_range = constraint(subdomain) if constraint_range.lower() > 0.0 or constraint_range.upper( ) < 0.0: return [] lowdomain = Box(subdomain[:constraint.argument_size()]) while lowdomain[-1].lower() >= self._domain[-1].lower(): if constraint(lowdomain).lower() > 0.0: return [] lowdomain[-1] = lowdomain[-1] - lowdomain[-1].width() for (id, constraint) in self._always_negative.items(): lowdomain = Box(subdomain[:constraint.argument_size()]) while lowdomain[-1].lower() >= self._domain[-1].lower(): if constraint(lowdomain).lower() > 0.0: return [] lowdomain[-1] = lowdomain[-1] - lowdomain[-1].width() #for (id,constraint,auxiliary) in self._ranged_negative.items(): # assert False range = Box(self._function(subdomain)) if range.radius() < eps: return [range] else: (subdomain1, subdomain2) = split(subdomain) return self.__outer_approximation(subdomain1,depth) + \ self.__outer_approximation(subdomain2,depth)
def straightLineDistance(node, next): x_2 = pow((node.point.x - next.point.x), 2) y_2 = pow((node.point.y - next.point.y), 2) return sqrt(x_2+y_2)
def heuristic(current, end): global h_const x_2 = pow((current.point.x - end.point.x), 2) y_2 = pow((current.point.y - end.point.y), 2) h = sqrt(x_2+y_2) return 20 * h / h_const
def eval_function(x, f, prime): return __builtin__.pow(x,3, prime)+f[0]*x+f[1]
def flip_point(x, j): """Sets x[i] to 1/x[i] whenever the ith bit of j is equal to 1.""" for i in range(0, len(x)): if j & __builtin__.pow(2, i): x[i] = 1.0 / x[i]
if ((x1 == x2) and (y1 != y2)): return (None, None) #corner case where y1==y2==0 if ((x1 == x2) and (y1 == 0) and (y2 == 0)): return (None, None) # definition of identity if (x1 is None): return (x2, y2) if (x2 is None): return (x1, y1) #m is tangent slope # if (modinv(((2*y1)%prime), prime)) is None: # print x1, y1, x2, y2 if ((x1 == x2) and (y1 == y2)): m = (((3 * __builtin__.pow(x1, 2, prime) + curve[0]) % prime) * modinv( ((2 * y1) % prime), prime)) % prime else: m = ((y2 - y1) % prime) * modinv((x2 - x1) % prime, prime) x3 = (__builtin__.pow(m, 2, prime) - x1 - x2) % prime y3 = (m * (x1 - x3) - y1) % prime return (long(x3), long(y3)) #need elliptic curve equivalent of successive squaring in order to complete in reasonable amount of time. We are working with an abelian group so this is allowable def elliptic_mul((x, y), n, curve, prime): if n == 0: return (None, None) if n == 1: return (x, y) elif (n % 2) == 1:
array=f.readline().split("/") alpha_x=array[0] alpha_y=array[1] beta_x=array[2] beta_y=array[3] return (long(alpha_x), long(alpha_y), long(beta_x), long(beta_y)) def read_private_key(name): f=open("."+name+"_private_key", 'r') f.readline() pk= long(f.readline()) f.close() return pk def is_point((x,y), curve, prime): if (__builtin__.pow(y,2,prime)%prime)==((__builtin__.pow(x,3,prime)+curve[0]*x+curve[1])%prime): return True else: return False #computes x value on the curve def encode_message(m,curve, prime): #give us a 1/2^20 chance of failure encoded=20*m for i in range(20): if is_quadratic_residue(long(eval_function(encoded, curve,prime))%prime, prime): break encoded=encoded+1 y=quadratic_residues(long(eval_function(encoded,curve, prime))%prime, prime) return (encoded, y[0])
def min_zero(n): """Compute the minumum binary digit of n which is equal to zero.""" k = 0 while n & __builtin__.pow(2, k): k += 1 return k