def semiCompositeIdempotentPartitions(n, factor_list): partitions_of_n = partitions(n, factor_list) ans = [] for (p, q) in partitions_of_n: pIsPrime = numbthy.is_prime(p) qIsPrime = numbthy.is_prime(q) if pIsPrime and qIsPrime or (not pIsPrime and not qIsPrime): continue pseudo = (p - 1) * (q - 1) lambda_n = carmichael_lambda_with_list(n, factor_list) if pseudo % lambda_n == 0: ans.append((p, q)) return ans
def fullyCompositeIdempotentPartitions(n, factor_list): partitions_of_n = partitions(n, factor_list) ans = [] for (p, q) in partitions_of_n: if numbthy.is_prime(p): continue if numbthy.is_prime(q): continue pseudo = (p - 1) * (q - 1) lambda_n = carmichael_lambda_with_list(n, factor_list) if pseudo % lambda_n == 0: ans.append((p, q)) return ans
def GF(n, poly=[], var='x', fmtspec="p"): """A shorthand for generating finite fields. If poly is not specified then one will be chosen from a list of Conway polynomials.""" if numbthy.is_prime(n): if len(poly) < 2: return FiniteField(n, [1], var=var, fmtspec=fmtspec) else: return FiniteField( n, poly, var=var, fmtspec=fmtspec ) # Explicit characteristic and polynomial modulus else: # n is not prime - hope it's a prime power nfactors = numbthy.factor(n) if (len(nfactors) != 1): raise ValueError( 'GF({0}) only makes sense for {0} a prime power'.format(n)) p = nfactors[0][0] e = nfactors[0][1] # Prime p, exponent e if (len(poly) == 0): try: poly = readconway('CPimport.txt', p, e)[:-1] # Assume monic except (IOError, ValueError): print(" Look for non-Conway primitive polynomial") poly = findprimpoly(p, e) else: if nfactors[0][1] != len(poly): raise ValueError( 'Polynomial {0} does not have degree {1}'.format( poly + [1], nfactors[0][1])) return FiniteField(p, poly, var=var, fmtspec=fmtspec)
def problem2(N): N = decimal.Decimal(N) # Use this routine to find A value that fits for N A = int(N.sqrt()) + 1 index = 1 maxdiff = 2**19 # A - sqrt(N) < 2 ** 19 while (index < maxdiff): x2 = A * A - N x = int(x2.sqrt()) if x * x == x2: p, q = A - x, A + x if (p * q == N): if is_prime(p) == True and is_prime(q) == True: return p index += 1 A += 1 print "No solution" return 0, 0
def is_prime_function(): print("Testing -- is_prime function --") print("Using numpy.random generating {0} numbers".format(testnumber)) for n in range(testnumber): a = rd.randint(minimum, maximum) tf1 = nmtf.is_prime(tf.constant(a)) nm1 = nm.is_prime(a) print("Test {0}------------------------------".format(n + 1)) print("nmtf.is_prime({0}) = {1}".format(a, tf1.eval())) print(" nm.is_prime({0}) = {1}".format(a, nm1)) print("")
def GF(n,name='x',modulus=[]): if numbthy.is_prime(n): if len(modulus)<2: print "Haven't coded prime field GF({0}) yet - sorry".format(n) raise ValueError("Haven't coded prime field GF({0}) yet - sorry".format(n)) else: return FiniteField(n,modulus,name) # Explicit characteristic and polynomial modulus else: # n is not prime - hope it's a prime power nfactors = numbthy.factor(n) if (len(nfactors) != 1): raise ValueError('GF({0}) only makes sense for {0} a prime power'.format(n)) p = nfactors[0][0]; e = nfactors[0][1] # Prime p, exponent e if (len(modulus) == 0): try: modulus = readconway('CPimport.txt',p,e)[:-1] # Assume monic except(IOError,ValueError): print(" Look for non-Conway primitive polynomial") modulus = findprimpoly(p,e) return FiniteField(p,modulus,name)
def GF(n, poly=[], var='x', fmtspec="p"): """A shorthand for generating finite fields. If poly is not specified then one will be chosen from a list of Conway polynomials.""" if numbthy.is_prime(n): if len(poly)<2: return FiniteField(n,[1],var=var,fmtspec=fmtspec) else: return FiniteField(n,poly,var=var,fmtspec=fmtspec) # Explicit characteristic and polynomial modulus else: # n is not prime - hope it's a prime power nfactors = numbthy.factor(n) if (len(nfactors) != 1): raise ValueError('GF({0}) only makes sense for {0} a prime power'.format(n)) p = nfactors[0][0]; e = nfactors[0][1] # Prime p, exponent e if (len(poly) == 0): try: poly = readconway('CPimport.txt',p,e)[:-1] # Assume monic except(IOError,ValueError): print(" Look for non-Conway primitive polynomial") poly = findprimpoly(p,e) else: if nfactors[0][1] != len(poly): raise ValueError('Polynomial {0} does not have degree {1}'.format(poly+[1],nfactors[0][1])) return FiniteField(p,poly,var=var,fmtspec=fmtspec)
def GF(n, name='x', modulus=[]): if numbthy.is_prime(n): if len(modulus) < 2: return FiniteField(n, [1], name) else: return FiniteField( n, modulus, name) # Explicit characteristic and polynomial modulus else: # n is not prime - hope it's a prime power nfactors = numbthy.factor(n) if (len(nfactors) != 1): raise ValueError( 'GF({0}) only makes sense for {0} a prime power'.format(n)) p = nfactors[0][0] e = nfactors[0][1] # Prime p, exponent e if (len(modulus) == 0): try: modulus = readconway('CPimport.txt', p, e)[:-1] # Assume monic except (IOError, ValueError): print(" Look for non-Conway primitive polynomial") modulus = findprimpoly(p, e) return FiniteField(p, modulus, name)
def test_is_prime(self): for testcase in ((1, False), (0, False), (-2, True), (19, True), (1236, False), (1237, True), (321197185, False)): self.assertEqual(numbthy.is_prime(testcase[0]), testcase[1])