Beispiel #1
0
    def __init__(self, integer, factors=None):
        """
        FactoredInteger(integer [, factors])

        If factors is given, it is a dict of type {prime:exponent}
        and the product of prime**exponent is equal to the integer.
        Otherwise, factorization is carried out in initialization.
        """
        self.integer = long(integer)
        if factors is None:
            self.factors = dict(methods.factor(self.integer))
        else:
            self.factors = dict(factors)
Beispiel #2
0
    def __init__(self, integer, factors=None):
        """
        FactoredInteger(integer [, factors])

        If factors is given, it is a dict of type {prime:exponent}
        and the product of prime**exponent is equal to the integer.
        Otherwise, factorization is carried out in initialization.
        """
        self.integer = int(integer)
        if factors is None:
            self.factors = dict(methods.factor(self.integer))
        else:
            self.factors = dict(factors)
Beispiel #3
0
 def elementOrder(self, element):
     """ Compute order of element of group.
     """
     # FIXME: Undefined variable 'grouporder'
     if self._orderfactor is None:
         self._orderfactor = factor_methods.factor(grouporder)
     order = 1
     for p, e in self._orderfactor:
         b = self.pow(element, (self.order // (p**e)))
         while b != self.one:
             order = order * p
             b = self.pow(b, p)
     return order
 def testFactorSpecifyMethod(self):
     self.assertEqual([(2, 2), (3, 1), (5, 1)], mthd.factor(60, method='t'))
     self.assertEqual([(2, 2), (3, 1), (5, 1)],
                      mthd.factor(60, method='trial'))
     self.assertEqual([(19, 1), (101, 1)], mthd.factor(1919, method='p'))
     self.assertEqual([(19, 1), (101, 1)], mthd.factor(1919, method='pmom'))
     p = 4 * 6133 + 1
     self.assertEqual([(p, 1), (154858631, 1)],
                      mthd.factor(p * 154858631, 'm'))
     self.assertEqual([(p, 1), (154858631, 1)],
                      mthd.factor(p * 154858631, 'e'))
     self.assertEqual([(2, 2), (3, 1), (5, 1)], mthd.factor(60, method='r'))
 def elementorder(self, elem):
     """
     Find and return the order of the element in the group.
     """
     assert hasattr(self, grouporder), "tell me the group order!"
     if elem not in self:
         raise ValueError("%s is not in the group." % elem)
     if self._orderfactor is None:
         self._orderfactor = factor_methods.factor(self.grouporder)
     o = 1
     for p, e in self._orderfactor:
         b = self.op2(elem, self.grouporder // (p**e))
         while b != self.unity:
             o = o * p
             b = self.op2(b, p)
     return o
Beispiel #6
0
 def order(self, elem):
     """
     Find and return the order of the element in the multiplicative
     group of the field.
     """
     if not elem:
         raise ValueError("zero is not in the group.")
     grouporder = card(self) - 1
     if self._orderfactor is None:
         self._orderfactor = factor_methods.factor(grouporder)
     o = 1
     for p, e in self._orderfactor:
         b = elem**(grouporder // (p**e))
         while b != self.one:
             o = o * p
             b = b**p
     return o
Beispiel #7
0
 def order(self):
     """
     Find and return the order of the element in the multiplicative
     group of F_p.
     """
     if self.n == 0:
         raise ValueError("zero is not in the group.")
     grouporder = self.m - 1
     if not hasattr(self, "orderfactor"):
         self.orderfactor = factor_methods.factor(grouporder)
     o = 1
     for p, e in self.orderfactor:
         b = self**(grouporder // (p**e))
         while b.n != 1:
             o = o * p
             b = b**p
     return o
Beispiel #8
0
def getT(t):
    dt = map(lambda x: x - min(t), t)

    try:
        while 1:
            dt.remove(0)
    except:
        pass

    factors = methods.factor(min(dt))

    T = 1
    for n, m in factors:
        while max(map(lambda x: x % n**m, dt)) > 0:
            m -= 1
        T = T * n**m

    return T
 def testFactor(self):
     # default method
     p = 4 * 6133 + 1
     result = mthd.factor(p * 154858631)
     self.assertEqual([(p, 1), (154858631, 1)], result)
Beispiel #10
0
    filename = sys.argv[1]
lines = [line.strip() for line in open(filename)]

# process the cases
cases = int(lines[0])
offset = 1
for case in range(1, cases + 1):

    # extract the details
    tokens = lines[offset].split(" ")
    A = int(tokens[0])
    B = int(tokens[1])
    P = int(tokens[2])

    # get the prime factors of P into a set
    P_factors = get_factor_set(factor(P))
    
    # process the range
    nums = range(A, B+1)
    groups = DisjointSet(nums)
    for num1i in range(0, len(nums)):
        for num2i in range(1+num1i, len(nums)):
             if groups.joined(nums[num1i], nums[num2i]):
                 continue
             num1_factors = factor(nums[num1i])
             num1_factor_set = get_factor_set(num1_factors)
             num2_factors = factor(nums[num2i])
             num2_factor_set = get_factor_set(num2_factors)
             common = False
             for num1_factor in num1_factors:
                 if num1_factor[0] < P: