コード例 #1
0
 def testModPow(self):
   self.assertRaises(number.NegativeNumberNotAllowedException, number.modPow, 1, 1, -1);
   self.assertRaises(number.NegativeNumberNotAllowedException, number.modPow, 1, -1, 1);
   self.assertRaises(number.NegativeNumberNotAllowedException, number.modPow, -1, 1, 1);
   self.assertEquals(0, number.modPow(2, 2, 0))
   self.assertEquals(1, number.modPow(2, 0, 2))
   self.assertEquals(0, number.modPow(0, 2, 2))
   self.assertEquals(1, number.modPow(2, 1092, 1093 ** 2))
コード例 #2
0
ファイル: primes.py プロジェクト: tomwhite/gaussian-primes
def primes(n):

  # 1
  if number.integralPower(n):
    return 0

  # 2
  r = 2

  # 3
  while r < n:

    # 4
    if number.gcd(n, r) != 1:
      return 0

    # 5
    if r in number.SMALL_PRIMES:

      # 6
      factors = number.factor(r - 1)
      if len(factors) == 0:
        q = 0 # hack
      else:
        q = factors[-1]

      # 7
      if (q >= 4 * math.sqrt(r) * number.lg(n)) and (number.modPow(n, (r - 1) / q, r) != 1):

        # 8
        break

    # 9
    r = r + 1

  # 10

  print "r for ", n, ":", r
  return r

  # 11
  #for a in range(1, int(2 * math.sqrt(r) * number.lg(n)) + 2)):

    # 12
    #poly = [-1] + [0] * (r - 1) + [1]
    #lhs = polynomial.modPow([-a, 1], n, poly)
    #rhs = polynomial.mod([-a] + [0] * (n - 1) + [1], poly)
    #if lhs != rhs:
    #  return 0

  # 13
  return 1