Esempio n. 1
0
    def alternateRep(self):

        global glbl_S
        posX = 0
        posY = 0
        theLength = len(self.theData)
        glbl_S = int(sr(theLength))

        self.theBoard = [[0 for x in range(glbl_S)] for y in range(glbl_S)]

        for el in self.theData:

            aNewLetter = None

            if (el != ' '):

                aNewLetter = el

            else:

                newLet = 0

            self.theBoard[posX][posY] = aNewLetter

            if posX < glbl_S - 1:

                posX += 1

            else:

                posY += 1
                posX = 0
Esempio n. 2
0
def find_factors(num):
    step = 2
    for i in range(step, int(sr(num) + 1)):
        if num % step == 0:
            while num % step == 0:
                num /= step
                print(step, end=" ")
        step += 1
Esempio n. 3
0
def is_prime(n):
    if n==2:
        return True
    if n<2:
        return False
    for i in range(2,int(sr(n)+1)):
        if n%i==0:
            return False
    return True
Esempio n. 4
0
def is_prime(n,
             depth=0):  # Similar to factor(), except abort after first factor
    '''\
Returns ... if a manual depth is given and there are no factors below that depth.
Tell me if you think of a better idea.'''
    n = _positive(n, "is_prime")

    if n in _primes[:5]: return True
    # Short circuit for very small primes, where sqrt() is long relative to
    # searching the list as below
    if n == 1 or n & 1 == 0: return False

    sqrt = int(sr(n)) + 1

    for p in _primes:
        if p > sqrt:
            return True
        if n % p == 0:
            return False

    # Not done yet, do some mangled sort of trial division
    if not prp(
            n):  # 20 of the best prp test implemented here (Miller Rabin ATM)
        return False
    #print("Mangled trial division", n, sqrt, depth)
    start = (_depth + 1) | 1  # start = next_odd(_depth)
    end = depth + 1 if sqrt > depth > 1 else sqrt + 1

    sstart = sr(start)
    if _primes[-1] < sstart:
        sieve_max = nt._count  # Use whole list
    else:
        sieve_max = _primes.index(next_prime(sstart))
    #print(n, sqrt, start, end, sstart, _primes[-1], _count, sieve_max)
    for i in range(start, end, 2):
        for p in _primes[:sieve_max]:  # Sieve div candidates
            if i % p == 0:
                break
        else:
            #print(i, end, i/end)
            if n % i == 0:
                return False
    return None if sqrt > depth > 1 else True
Esempio n. 5
0
def area_of_triangle(a, b, c):
    if a + b > c and a + c > b and b + c > a:
        perimeter = a + b + c
        print('周长:%.2f' % (perimeter))
        # sp is short for semi perimeter
        sp = perimeter / 2
        area = sr(sp * (sp - a) * (sp - b) * (sp - c))
        print('面积:%.2f' % (area))
    else:
        print('边长参数无法构成三角形')
Esempio n. 6
0
def is_prime(n, depth=0): # Similar to factor(), except abort after first factor
     '''\
Returns ... if a manual depth is given and there are no factors below that depth.
Tell me if you think of a better idea.'''
     n = _positive(n, "is_prime")

     if n in _primes[:5]: return True
     # Short circuit for very small primes, where sqrt() is long relative to
     # searching the list as below
     if n == 1 or n & 1 == 0: return False

     sqrt = int(sr(n))+1

     for p in _primes:
          if p > sqrt:
               return True
          if n % p == 0:
               return False

     # Not done yet, do some mangled sort of trial division
     if not prp(n): # 20 of the best prp test implemented here (Miller Rabin ATM)
          return False
     #print("Mangled trial division", n, sqrt, depth)
     start = (_depth+1) | 1 # start = next_odd(_depth)
     end = depth+1 if sqrt > depth > 1 else sqrt+1

     sstart = sr(start)
     if _primes[-1] < sstart:
          sieve_max = nt._count # Use whole list
     else:
          sieve_max = _primes.index(next_prime(sstart))
     #print(n, sqrt, start, end, sstart, _primes[-1], _count, sieve_max)
     for i in range(start, end, 2):
          for p in _primes[:sieve_max]: # Sieve div candidates
               if i % p == 0:
                    break
          else:
               #print(i, end, i/end)
               if n % i == 0:
                    return False
     return None if sqrt > depth > 1 else True
Esempio n. 7
0
def factor(num, depth=0, factors=None, start=3):
     num = _positive(num, "factor")

     if factors is None:
          factors = Factors()
          factors.num = num

     if _depth > depth > 1 : depth = _depth

     if num == 1:
          if not factors.full:
               print("Warning: there is a composite cofactor!")
          return factors

     if num & 1 == 0:
          factors[2] = quick_pow_of_two(num)
          return factor(num>>factors[2], depth, factors, start)

     try:
          sqrt = int(sr(num)) + 1
     except OverflowError:
          # If we're dealing with number larger than 1024 bits, we can't use
          # the math module to get the square root; instead, look for small
          # factors only if depth was set, else re-barf.
          if depth > 1:
               sqrt = depth + 1 # Code below checks that sqrt > depth
          else:
               raise OverflowError('num is too big for math.sqrt() to handle. '+
                    'If you still want to look for small factors, set the depth.')

     if start < _depth:
          ind = _primes.index(next_prime(start))
     else:
          ind = _count # == len(_primes)

     # First div with known primes
     for p in _primes[ind:]:
          if p > sqrt: # Then num is prime
               factors[num] += 1
               return factors
          if num % p == 0:
               factors[p] += 1
               return factor(num//p, depth, factors, p) # Restart sieving at p

     # Beyond our primes
     start = max(start, (_depth+1)|1) # start = max(start, next_odd(_depth))
     end = depth+1 if sqrt > depth > 1 else sqrt+1
     for i in range(start, end, 2):
          #for p in _primes:
          #     if i % p == 0:
          #          break
          #else:
               if num % i == 0:
                    factors[i] += 1
                    return factor(num//i, depth, factors, i) # Restart "sieving" at i
     # Dropped out of loop, num is prime (or no factors below depth)
     if sqrt > depth > 1: # Manual depth, could be missed factor
          if miller_bach(num): # 2*ln(num)^2 Miller-Rabin tests
                    # Under the GRH, this is a deterministic primality test
               factors[num] += 1
               print(("Warning: {} passed the Miller Bach test, which guarantees"
                    +" primality under the GRH").format(num))
          else:
               factors.full = False
               print(("Warning: {} is composite, but the trial factoring depth {}"
                    +" has been reached.").format(num, depth))
               factors[num] += 1
     else: # Guaranteed prime
          factors[num] += 1
     return factors
Esempio n. 8
0
def factor(num, depth=0, factors=None, start=3):
    num = _positive(num, "factor")

    if factors is None:
        factors = Factors()
        factors.num = num

    if _depth > depth > 1: depth = _depth

    if num == 1:
        if not factors.full:
            print("Warning: there is a composite cofactor!")
        return factors

    if num & 1 == 0:
        factors[2] = quick_pow_of_two(num)
        return factor(num >> factors[2], depth, factors, start)

    try:
        sqrt = int(sr(num)) + 1
    except OverflowError:
        # If we're dealing with number larger than 1024 bits, we can't use
        # the math module to get the square root; instead, look for small
        # factors only if depth was set, else re-barf.
        if depth > 1:
            sqrt = depth + 1  # Code below checks that sqrt > depth
        else:
            raise OverflowError(
                'num is too big for math.sqrt() to handle. ' +
                'If you still want to look for small factors, set the depth.')

    if start < _depth:
        ind = _primes.index(next_prime(start))
    else:
        ind = _count  # == len(_primes)

    # First div with known primes
    for p in _primes[ind:]:
        if p > sqrt:  # Then num is prime
            factors[num] += 1
            return factors
        if num % p == 0:
            factors[p] += 1
            return factor(num // p, depth, factors, p)  # Restart sieving at p

    # Beyond our primes
    start = max(start,
                (_depth + 1) | 1)  # start = max(start, next_odd(_depth))
    end = depth + 1 if sqrt > depth > 1 else sqrt + 1
    for i in range(start, end, 2):
        #for p in _primes:
        #     if i % p == 0:
        #          break
        #else:
        if num % i == 0:
            factors[i] += 1
            return factor(num // i, depth, factors,
                          i)  # Restart "sieving" at i
    # Dropped out of loop, num is prime (or no factors below depth)
    if sqrt > depth > 1:  # Manual depth, could be missed factor
        if miller_bach(num):  # 2*ln(num)^2 Miller-Rabin tests
            # Under the GRH, this is a deterministic primality test
            factors[num] += 1
            print(
                ("Warning: {} passed the Miller Bach test, which guarantees" +
                 " primality under the GRH").format(num))
        else:
            factors.full = False
            print(
                ("Warning: {} is composite, but the trial factoring depth {}" +
                 " has been reached.").format(num, depth))
            factors[num] += 1
    else:  # Guaranteed prime
        factors[num] += 1
    return factors
Esempio n. 9
0
def find_smallest(n, cmp):
    while n >= (-sr(cmp)):
        n -= 1
    print(n)