Exemplo n.º 1
0
def getNthSquareTriangularNumber( n ):
    neededPrecision = int( real_int( n ) * 3.5 )  # determined by experimentation

    if mp.dps < neededPrecision:
        setAccuracy( neededPrecision )

    sqrt2 = sqrt( 2 )

    return nint( power( fdiv( fsub( power( fadd( 1, sqrt2 ), fmul( 2, n ) ),
                                           power( fsub( 1, sqrt2 ), fmul( 2, n ) ) ),
                                    fmul( 4, sqrt2 ) ), 2 ) )
Exemplo n.º 2
0
def parseInputValue( term, inputRadix ):
    if isinstance( term, mpf ):
        return term

    innerChars = term[ 1 : -1 ]

    # this helps us parse dates
    if '/' in innerChars:
        term = term.replace( '/', '-' )
        innerChars = term[ 1 : -1 ]

    # 'e' implies scientific notation, which isn't a date regardless
    if ( 'e' not in innerChars ):
        # 'd' means a dice expression, '[' means a build_number expression, so don't treat it as a date
        if ( ( '-' in innerChars ) or ( ':' in innerChars ) ) and ( 'd' not in term ) and ( '[' not in term ):
            # try:
                datetime = arrow.get( term )
                datetime = RPNDateTime( datetime.year, datetime.month, datetime.day,
                                        datetime.hour, datetime.minute, datetime.second,
                                        datetime.microsecond )
            # except:
            #     raise ValueError( 'error parsing datetime' )

                return datetime

    if term == '0':
        return mpmathify( 0 )

    # ignore commas
    term = ''.join( [ i for i in term if i not in ',' ] )

    if term[ 0 ] == '\\':
        term = term[ 1 : ]
        ignoreSpecial = True
    else:
        ignoreSpecial = False

    if '.' in term:
        if inputRadix == 10:
            newPrecision = len( term ) + 1

            if mp.dps < newPrecision:
                setAccuracy( newPrecision )

            return mpmathify( term )

        decimal = term.find( '.' )
    else:
        decimal = len( term )

    negative = term[ 0 ] == '-'

    if negative:
        start = 1
    else:
        if term[ 0 ] == '+':
            start = 1
        else:
            start = 0

    integer = term[ start : decimal ]
    mantissa = term[ decimal + 1 : ]

    # check for hex, then binary, then octal, otherwise a plain old decimal integer
    if not ignoreSpecial and mantissa == '':
        if integer[ 0 ] == '0':
            if len( integer ) == 1:
                return mpmathify( 0 )

            if integer[ 1 ] in 'Xx':
                # set the precision big enough to handle this value
                newPrecision = math.ceil( ( math.log10( 16 ) * ( len( integer ) - 2 ) ) ) + 1

                if mp.dps < newPrecision:
                    setAccuracy( newPrecision )

                return mpmathify( int( integer[ 2 : ], 16 ) )
            elif integer[ -1 ] in 'bB':
                # set the precision big enough to handle this value
                newPrecision = math.ceil( math.log10( 2 ) * ( len( integer ) - 1 ) ) + 1

                if mp.dps < newPrecision:
                    setAccuracy( newPrecision )

                integer = integer[ : -1 ]
                return mpmathify( int( integer, 2 ) * ( -1 if negative else 1 ) )
            else:
                integer = integer[ 1 : ]

                return mpmathify( int( integer, 8 ) )
        if integer[ 0 ] == '1' and integer[ -1 ] in 'bB':
            # set the precision big enough to handle this value
            newPrecision = math.ceil( math.log10( 2 ) * ( len( integer ) - 1 ) ) + 1

            if mp.dps < newPrecision:
                setAccuracy( newPrecision )

            integer = integer[ : -1 ]
            return mpmathify( int( integer, 2 ) * ( -1 if negative else 1 ) )
        elif inputRadix == 10:
            newPrecision = len( integer ) + 1

            if mp.dps < newPrecision:
                setAccuracy( newPrecision )

            return fneg( integer ) if negative else mpmathify( integer )

    # finally, we have a non-radix 10 number to parse
    result = convertToBase10( integer, mantissa, inputRadix )
    return fneg( result ) if negative else mpmathify( result )
Exemplo n.º 3
0
def getFactors( n ):
    '''
    A factorization of *Nptr into prime and q-prime factors is first obtained.

    Selfridge's primality test is then applied to any q-prime factors; the test
    is applied repeatedly until either a q-prime is found to be composite or
    likely to be composite (in which case the initial factorization is doubtful
    and an extra base should be used in Miller's test) or we run out of q-primes,
    in which case every q-prime factor of *Nptr is certified as prime.

    Returns a list of tuples where each tuple is a prime factor and an exponent.
    '''

    verbose = g.verbose

    if real( n ) < -1:
        return [ ( -1, 1 ) ] + getFactors( fneg( n ) )
    elif n == -1:
        return [ ( -1, 1 ) ]
    elif n == 0:
        return [ ( 0, 1 ) ]
    elif n == 1:
        return [ ( 1, 1 ) ]

    target = int( n )

    dps = ceil( log( n ) )

    if dps > mp.dps:
        setAccuracy( dps )

    if target > g.minValueToCache:
        if g.factorCache is None:
            loadFactorCache( )

        if target in g.factorCache:
            if verbose:
                print( 'cache hit:', target )

            return g.factorCache[ target ]

    smallFactors, largeFactors, qPrimes = getPrimeFactors( int( n ), verbose )

    if qPrimes:
        if verbose:
            print( 'testing q-primes for primality' )
            print( '--' )

        i = 0

        for qPrime in qPrimes:
            t = doSelfridgeTest( qPrime[ 0 ], verbose )

            if not t:
                print( 'do FACTOR() again with an extra base' )
                return 0

        if verbose:
            print( 'all q-primes are primes:', n, 'has the following factorization:' )
            print( )

            for i in smallFactors:
                print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] )

            for i in largeFactors:
                print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] )
    else:
        if verbose:
            print( 'NO Q-PRIMES:' )
            print( )
            print( n, 'has the following factorization:' )

            for i in smallFactors:
                print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] )

            for i in largeFactors:
                print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] )

    result = [ ]

    result.extend( smallFactors )
    result.extend( largeFactors )

    if g.factorCache is not None:
        product = int( fprod( [ power( i[ 0 ], i[ 1 ] ) for i in largeFactors ] ) )

        if product not in g.factorCache:
            g.factorCache[ product ] = largeFactors
            g.factorCacheIsDirty = True

        if n > g.minValueToCache and n not in g.factorCache:
            g.factorCache[ n ] = result
            g.factorCacheIsDirty = True

    return result
Exemplo n.º 4
0
def getFactors( n ):
    verbose = g.verbose

    if real( n ) < -1:
        return [ ( -1, 1 ) ] + getFactors( fneg( n ) )
    elif n == -1:
        return [ ( -1, 1 ) ]
    elif n == 0:
        return [ ( 0, 1 ) ]
    elif n == 1:
        return [ ( 1, 1 ) ]

    target = int( n )

    dps = ceil( log( n ) )

    if dps > mp.dps:
        setAccuracy( dps )

    if target > g.minValueToCache:
        if g.factorCache is None:
            loadFactorCache( )

        if target in g.factorCache:
            if verbose:
                print( 'cache hit:', target )

            return g.factorCache[ target ]

    smallFactors, largeFactors, qPrimes = getPrimeFactors( int( n ), verbose )

    if qPrimes:
        if verbose:
            print( 'testing q-primes for primality' )
            print( '--' )

        i = 0

        for qPrime in qPrimes:
            t = doSelfridgeTest( qPrime[ 0 ], verbose )

            if not t:
                print( 'do FACTOR() again with an extra base' )
                return 0

        if verbose:
            print( 'all q-primes are primes:', n, 'has the following factorization:' )
            print( )

            for i in smallFactors:
                print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] )

            for i in largeFactors:
                print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] )
    else:
        if verbose:
            print( 'NO Q-PRIMES:' )
            print( )
            print( n, 'has the following factorization:' )

            for i in smallFactors:
                print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] )

            for i in largeFactors:
                print( 'prime factor:', i[ 0 ], 'exponent:', i[ 1 ] )

    result = [ ]

    result.extend( smallFactors )
    result.extend( largeFactors )

    if g.factorCache is not None:
        product = int( fprod( [ power( i[ 0 ], i[ 1 ] ) for i in largeFactors ] ) )

        if product not in g.factorCache:
            g.factorCache[ product ] = largeFactors
            g.factorCacheIsDirty = True

        if n > g.minValueToCache and n not in g.factorCache:
            g.factorCache[ n ] = result
            g.factorCacheIsDirty = True

    return result