Example #1
0
def phi(n):

    #*****************************************************************************80
    #
    ## PHI computes the number of relatively prime predecessors of an integer.
    #
    #  Definition:
    #
    #    PHI(N) is the number of integers between 1 and N which are
    #    relatively prime to N.  I and J are relatively prime if they
    #    have no common factors.  The function PHI(N) is known as
    #    "Euler's totient function".
    #
    #    By convention, 1 and N are relatively prime.
    #
    #  First values:
    #
    #     N  PHI(N)
    #
    #     1    1
    #     2    1
    #     3    2
    #     4    2
    #     5    4
    #     6    2
    #     7    6
    #     8    4
    #     9    6
    #    10    4
    #    11   10
    #    12    4
    #    13   12
    #    14    6
    #    15    8
    #    16    8
    #    17   16
    #    18    6
    #    19   18
    #    20    8
    #
    #  Formula:
    #
    #    PHI(U*V) = PHI(U) * PHI(V) if U and V are relatively prime.
    #
    #    PHI(P^K) = P^(K-1) * ( P - 1 ) if P is prime.
    #
    #    PHI(N) = N * Product ( P divides N ) ( 1 - 1 / P )
    #
    #    N = Sum ( D divides N ) PHI(D).
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    25 February 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer N, the value to be analyzed.
    #
    #    Output, integer VALUE, the value of PHI(N).  If N is less than
    #    or equal to 0, PHI will be returned as 0.  If there is not enough
    #    room for full factoring of N, PHI will be returned as -1.
    #
    from i4_factor import i4_factor

    if (n <= 0):
        value = 0
        return value

    if (n == 1):
        value = 1
        return value


#
#  Factor N.
#
    nfactor, factor, power, nleft = i4_factor(n)

    if (nleft != 1):
        print ''
        print 'PHI - Fatal error!'
        print '  Not enough factorization space.'

    value = 1
    for i in range(0, nfactor):
        value = value * factor[i]**(power[i] - 1) * (factor[i] - 1)

    return value
def legendre_symbol ( q, p ):

#*****************************************************************************80
#
## LEGENDRE_SYMBOL evaluates the Legendre symbol (Q/P).
#
#  Definition:
#
#    Let P be an odd prime.  Q is a QUADRATIC RESIDUE modulo P
#    if there is an integer R such that R^2 = Q ( mod P ).
#    The Legendre symbol ( Q / P ) is defined to be:
#
#      + 1 if Q ( mod P ) /= 0 and Q is a quadratic residue modulo P,
#      - 1 if Q ( mod P ) /= 0 and Q is not a quadratic residue modulo P,
#        0 if Q ( mod P ) == 0.
#
#    We can also define ( Q / P ) for P = 2 by:
#
#      + 1 if Q ( mod P ) /= 0
#        0 if Q ( mod P ) == 0
#
#  Example:
#
#    (0/7) =   0
#    (1/7) = + 1  ( 1^2 = 1 mod 7 )
#    (2/7) = + 1  ( 3^2 = 2 mod 7 )
#    (3/7) = - 1
#    (4/7) = + 1  ( 2^2 = 4 mod 7 )
#    (5/7) = - 1
#    (6/7) = - 1
#
#  Note:
#
#    For any prime P, exactly half of the integers from 1 to P-1
#    are quadratic residues.
#
#    ( 0 / P ) = 0.
#
#    ( Q / P ) = ( mod ( Q, P ) / P ).
#
#    ( Q / P ) = ( Q1 / P ) * ( Q2 / P ) if Q = Q1 * Q2.
#
#    If Q is prime, and P is prime and greater than 2, then:
#
#      if ( Q == 1 ) then
#
#        ( Q / P ) = 1
#
#      else if ( Q == 2 ) then
#
#        ( Q / P ) = + 1 if mod ( P, 8 ) = 1 or mod ( P, 8 ) = 7,
#        ( Q / P ) = - 1 if mod ( P, 8 ) = 3 or mod ( P, 8 ) = 5.
#
#      else
#
#        ( Q / P ) = - ( P / Q ) if Q = 3 ( mod 4 ) and P = 3 ( mod 4 ),
#                  =   ( P / Q ) otherwise.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    16 February 2015
#
#  Author:
#
#    John Burkardt
#
#  Reference:
#
#    Charles Pinter,
#    A Book of Abstract Algebra,
#    McGraw Hill, 1982, pages 236-237.
#
#    Daniel Zwillinger,
#    CRC Standard Mathematical Tables and Formulae,
#    30th Edition,
#    CRC Press, 1996, pages 86-87.
#
#  Parameters:
#
#    Input, integer Q, an integer whose Legendre symbol with
#    respect to P is desired.
#
#    Input, integer P, a prime number, greater than 1, with respect
#    to which the Legendre symbol of Q is desired.
#
#    Output, integer L, the Legendre symbol (Q/P).
#    Ordinarily, L will be -1, 0 or 1.
#    L = -2, P is less than or equal to 1.
#    L = -3, P is not prime.
#    L = -4, the internal stack of factors overflowed.
#    L = -5, not enough factorization space.
#
  import numpy as np
  from i4_factor import i4_factor
  from i4_is_prime import i4_is_prime
  from sys import exit

  l = 0
#
#  P must be greater than 1.
#
  if ( p <= 1 ):
    print ''
    print 'LEGENDRE_SYMBOL - Fatal error!'
    print '  P must be greater than 1.'
    l = -2
    exit ( 'LEGENDRE_SYMBOL - Fatal error!' )
#
#  P must be prime.
#
  if ( not ( i4_is_prime ( p ) ) ):
    print ''
    print 'LEGENDRE_SYMBOL - Fatal error!'
    print '  P is not prime.'
    l = -3
    exit ( 'LEGENDRE_SYMBOL - Fatal error!' )
#
#  ( k*P / P ) = 0.
#
  if ( ( q % p ) == 0 ):
    l = 0
    return l
#
#  For the special case P = 2, (Q/P) = 1 for all odd numbers.
#
  if ( p == 2 ):
    l = 1
    return l

#
#  Make a copy of Q, and force it to be nonnegative.
#
  qq = q

  while ( qq < 0 ):
    qq = qq + p

  nstack = 0
  pstack = np.zeros ( 100 )
  qstack = np.zeros ( 100 )
  pp = p
  l = 1

  while ( True ):

    qq = ( qq % pp )
#
#  Decompose QQ into factors of prime powers.
#
    nfactor, factor, power, nleft = i4_factor ( qq )

    if ( nleft != 1 ):
      print ''
      print 'LEGENDRE_SYMBOL - Fatal error!'
      print '  Not enough factorization space.'
      l = -5
      exit ( 'LEGENDRE_SYMBOL - Fatal error!' )
#
#  Each factor which is an odd power is added to the stack.
#
    nmore = 0

    for i in range ( 0, nfactor ):

      if ( ( power[i] % 2 ) == 1 ):

        nmore = nmore + 1
        pstack[nstack] = pp
        qstack[nstack] = factor[i]
        nstack = nstack + 1

    hop = False

    if ( nmore != 0 ):

      nstack = nstack - 1
      qq = qstack[nstack]
#
#  Check for a QQ of 1 or 2.
#
      if ( qq == 1 ):

        l = + 1 * l

      elif ( qq == 2 and ( ( pp % 8 ) == 1 or ( pp % 8 ) == 7 ) ):

        l = + 1 * l

      elif ( qq == 2 and ( ( pp % 8 ) == 3 or ( pp % 8 ) == 5 ) ):

        l = - 1 * l

      else:

        if ( ( pp % 4 ) == 3 and ( qq % 4 ) == 3 ):
          l = - 1 * l

        rr = pp
        pp = qq
        qq = rr

        hop = True
#
#  If the stack is empty, we're done.
#
    if ( not hop ):

      if ( nstack == 0 ):
        break
#
#  Otherwise, get the last P and Q from the stack, and process them.
#
      nstack = nstack - 1
      pp = pstack[nstack]
      qq = qstack[nstack]

  return l
Example #3
0
def sigma ( n ):

#*****************************************************************************80
#
## SIGMA returns the value of SIGMA(N), the divisor sum.
#
#  Definition:
#
#    SIGMA(N) is the sum of the distinct divisors of N, including 1 and N.
#
#  First values:
#
#     N  SIGMA(N)
#
#     1    1
#     2    3
#     3    4
#     4    7
#     5    6
#     6   12
#     7    8
#     8   15
#     9   13
#    10   18
#    11   12
#    12   28
#    13   14
#    14   24
#    15   24
#    16   31
#    17   18
#    18   39
#    19   20
#    20   42
#
#  Formula:
#
#    SIGMA(U*V) = SIGMA(U) * SIGMA(V) if U and V are relatively prime.
#
#    SIGMA(P^K) = ( P^(K+1) - 1 ) / ( P - 1 ) if P is prime.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    26 February 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the value to be analyzed.
#
#    Output, integer VALUE, the value of SIGMA(N).  If N is less than
#    or equal to 0, VALUE will be returned as 0.  If there is not
#    enough room for factoring N, VALUE is returned as -1.
#
  from i4_factor import i4_factor

  maxfactor = 20

  if ( n <= 0 ):
    value = 0
    return value

  if ( n == 1 ):
    value = 1
    return value
#
#  Factor N.
#
  nfactor, factor, power, nleft = i4_factor ( n )

  if ( nleft != 1 ):
    print ''
    print 'SIGMA - Fatal error!'
    print '  Not enough factorization space.'

  value = 1
  for i in range ( 0, nfactor ):
    value = ( value * ( factor[i] ** ( power[i] + 1 ) - 1 ) ) \
      / ( factor[i] - 1 )

  return value
def legendre_symbol(q, p):

    #*****************************************************************************80
    #
    ## LEGENDRE_SYMBOL evaluates the Legendre symbol (Q/P).
    #
    #  Definition:
    #
    #    Let P be an odd prime.  Q is a QUADRATIC RESIDUE modulo P
    #    if there is an integer R such that R^2 = Q ( mod P ).
    #    The Legendre symbol ( Q / P ) is defined to be:
    #
    #      + 1 if Q ( mod P ) /= 0 and Q is a quadratic residue modulo P,
    #      - 1 if Q ( mod P ) /= 0 and Q is not a quadratic residue modulo P,
    #        0 if Q ( mod P ) == 0.
    #
    #    We can also define ( Q / P ) for P = 2 by:
    #
    #      + 1 if Q ( mod P ) /= 0
    #        0 if Q ( mod P ) == 0
    #
    #  Example:
    #
    #    (0/7) =   0
    #    (1/7) = + 1  ( 1^2 = 1 mod 7 )
    #    (2/7) = + 1  ( 3^2 = 2 mod 7 )
    #    (3/7) = - 1
    #    (4/7) = + 1  ( 2^2 = 4 mod 7 )
    #    (5/7) = - 1
    #    (6/7) = - 1
    #
    #  Note:
    #
    #    For any prime P, exactly half of the integers from 1 to P-1
    #    are quadratic residues.
    #
    #    ( 0 / P ) = 0.
    #
    #    ( Q / P ) = ( mod ( Q, P ) / P ).
    #
    #    ( Q / P ) = ( Q1 / P ) * ( Q2 / P ) if Q = Q1 * Q2.
    #
    #    If Q is prime, and P is prime and greater than 2, then:
    #
    #      if ( Q == 1 ) then
    #
    #        ( Q / P ) = 1
    #
    #      else if ( Q == 2 ) then
    #
    #        ( Q / P ) = + 1 if mod ( P, 8 ) = 1 or mod ( P, 8 ) = 7,
    #        ( Q / P ) = - 1 if mod ( P, 8 ) = 3 or mod ( P, 8 ) = 5.
    #
    #      else
    #
    #        ( Q / P ) = - ( P / Q ) if Q = 3 ( mod 4 ) and P = 3 ( mod 4 ),
    #                  =   ( P / Q ) otherwise.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    16 February 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Reference:
    #
    #    Charles Pinter,
    #    A Book of Abstract Algebra,
    #    McGraw Hill, 1982, pages 236-237.
    #
    #    Daniel Zwillinger,
    #    CRC Standard Mathematical Tables and Formulae,
    #    30th Edition,
    #    CRC Press, 1996, pages 86-87.
    #
    #  Parameters:
    #
    #    Input, integer Q, an integer whose Legendre symbol with
    #    respect to P is desired.
    #
    #    Input, integer P, a prime number, greater than 1, with respect
    #    to which the Legendre symbol of Q is desired.
    #
    #    Output, integer L, the Legendre symbol (Q/P).
    #    Ordinarily, L will be -1, 0 or 1.
    #    L = -2, P is less than or equal to 1.
    #    L = -3, P is not prime.
    #    L = -4, the internal stack of factors overflowed.
    #    L = -5, not enough factorization space.
    #
    import numpy as np
    from i4_factor import i4_factor
    from i4_is_prime import i4_is_prime
    from sys import exit

    l = 0
    #
    #  P must be greater than 1.
    #
    if (p <= 1):
        print ''
        print 'LEGENDRE_SYMBOL - Fatal error!'
        print '  P must be greater than 1.'
        l = -2
        exit('LEGENDRE_SYMBOL - Fatal error!')
#
#  P must be prime.
#
    if (not (i4_is_prime(p))):
        print ''
        print 'LEGENDRE_SYMBOL - Fatal error!'
        print '  P is not prime.'
        l = -3
        exit('LEGENDRE_SYMBOL - Fatal error!')
#
#  ( k*P / P ) = 0.
#
    if ((q % p) == 0):
        l = 0
        return l
#
#  For the special case P = 2, (Q/P) = 1 for all odd numbers.
#
    if (p == 2):
        l = 1
        return l

#
#  Make a copy of Q, and force it to be nonnegative.
#
    qq = q

    while (qq < 0):
        qq = qq + p

    nstack = 0
    pstack = np.zeros(100)
    qstack = np.zeros(100)
    pp = p
    l = 1

    while (True):

        qq = (qq % pp)
        #
        #  Decompose QQ into factors of prime powers.
        #
        nfactor, factor, power, nleft = i4_factor(qq)

        if (nleft != 1):
            print ''
            print 'LEGENDRE_SYMBOL - Fatal error!'
            print '  Not enough factorization space.'
            l = -5
            exit('LEGENDRE_SYMBOL - Fatal error!')
#
#  Each factor which is an odd power is added to the stack.
#
        nmore = 0

        for i in range(0, nfactor):

            if ((power[i] % 2) == 1):

                nmore = nmore + 1
                pstack[nstack] = pp
                qstack[nstack] = factor[i]
                nstack = nstack + 1

        hop = False

        if (nmore != 0):

            nstack = nstack - 1
            qq = qstack[nstack]
            #
            #  Check for a QQ of 1 or 2.
            #
            if (qq == 1):

                l = +1 * l

            elif (qq == 2 and ((pp % 8) == 1 or (pp % 8) == 7)):

                l = +1 * l

            elif (qq == 2 and ((pp % 8) == 3 or (pp % 8) == 5)):

                l = -1 * l

            else:

                if ((pp % 4) == 3 and (qq % 4) == 3):
                    l = -1 * l

                rr = pp
                pp = qq
                qq = rr

                hop = True
#
#  If the stack is empty, we're done.
#
        if (not hop):

            if (nstack == 0):
                break


#
#  Otherwise, get the last P and Q from the stack, and process them.
#
            nstack = nstack - 1
            pp = pstack[nstack]
            qq = qstack[nstack]

    return l
def jacobi_symbol ( q, p ):

#*****************************************************************************80
#
## JACOBI_SYMBOL evaluates the Jacobi symbol (Q/P).
#
#  Definition:
#
#    If P is prime, then
#
#      Jacobi Symbol (Q/P) = Legendre Symbol (Q/P)
#
#    Else 
#
#      let P have the prime factorization
#
#        P = Product ( 1 <= I <= N ) P(I)^E(I)
#
#      Jacobi Symbol (Q/P) =
#
#        Product ( 1 <= I <= N ) Legendre Symbol (Q/P(I))^E(I)
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    17 February 2015
#
#  Author:
#
#    John Burkardt
#
#  Reference:
#
#    Daniel Zwillinger,
#    CRC Standard Mathematical Tables and Formulae,
#    30th Edition,
#    CRC Press, 1996, pages 86-87.
#
#  Parameters:
#
#    Input, integer Q, an integer whose Jacobi symbol with
#    respect to P is desired.
#
#    Input, integer P, the number with respect to which the Jacobi
#    symbol of Q is desired.  P should be 2 or greater.
#
#    Output, integer J, the Jacobi symbol (Q/P).
#    Ordinarily, J will be -1, 0 or 1.
#    -2, not enough factorization space.
#    -3, an error during Legendre symbol calculation.
#
  from i4_factor import i4_factor
  from legendre_symbol import legendre_symbol
#
#  P must be greater than 1.
#
  if ( p <= 1 ):
    print ''
    print 'JACOBI_SYMBOL - Fatal error!'
    print '  P must be greater than 1.'
    j = -2
    return l
#
#  Decompose P into factors of prime powers.
#
  nfactor, factor, power, nleft = i4_factor ( p )

  if ( nleft != 1 ):
    print ''
    print 'JACOBI_SYMBOL - Fatal error!'
    print '  Not enough factorization space.'
    j = -2
    return j
#
#  Force Q to be nonnegative.
#
  qq = q

  while ( qq < 0 ):
    qq = qq + p
#
#  For each prime factor, compute the Legendre symbol, and
#  multiply the Jacobi symbol by the appropriate factor.
#
  j = 1
  for i in range ( 0, nfactor ):
    pp = factor[i]
    l = legendre_symbol ( qq, pp )
    if ( l < -1 ):
      print ''
      print 'JACOBI_SYMBOL - Fatal error!'
      print '  Error during Legendre symbol calculation.'
      j = -3

    j = j * l ** power[i]

  return j
Example #6
0
def jacobi_symbol(q, p):

    #*****************************************************************************80
    #
    ## JACOBI_SYMBOL evaluates the Jacobi symbol (Q/P).
    #
    #  Definition:
    #
    #    If P is prime, then
    #
    #      Jacobi Symbol (Q/P) = Legendre Symbol (Q/P)
    #
    #    Else
    #
    #      let P have the prime factorization
    #
    #        P = Product ( 1 <= I <= N ) P(I)^E(I)
    #
    #      Jacobi Symbol (Q/P) =
    #
    #        Product ( 1 <= I <= N ) Legendre Symbol (Q/P(I))^E(I)
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    17 February 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Reference:
    #
    #    Daniel Zwillinger,
    #    CRC Standard Mathematical Tables and Formulae,
    #    30th Edition,
    #    CRC Press, 1996, pages 86-87.
    #
    #  Parameters:
    #
    #    Input, integer Q, an integer whose Jacobi symbol with
    #    respect to P is desired.
    #
    #    Input, integer P, the number with respect to which the Jacobi
    #    symbol of Q is desired.  P should be 2 or greater.
    #
    #    Output, integer J, the Jacobi symbol (Q/P).
    #    Ordinarily, J will be -1, 0 or 1.
    #    -2, not enough factorization space.
    #    -3, an error during Legendre symbol calculation.
    #
    from i4_factor import i4_factor
    from legendre_symbol import legendre_symbol
    #
    #  P must be greater than 1.
    #
    if (p <= 1):
        print('')
        print('JACOBI_SYMBOL - Fatal error!')
        print('  P must be greater than 1.')
        j = -2
        return l
#
#  Decompose P into factors of prime powers.
#
    nfactor, factor, power, nleft = i4_factor(p)

    if (nleft != 1):
        print('')
        print('JACOBI_SYMBOL - Fatal error!')
        print('  Not enough factorization space.')
        j = -2
        return j
#
#  Force Q to be nonnegative.
#
    qq = q

    while (qq < 0):
        qq = qq + p


#
#  For each prime factor, compute the Legendre symbol, and
#  multiply the Jacobi symbol by the appropriate factor.
#
    j = 1
    for i in range(0, nfactor):
        pp = factor[i]
        l = legendre_symbol(qq, pp)
        if (l < -1):
            print('')
            print('JACOBI_SYMBOL - Fatal error!')
            print('  Error during Legendre symbol calculation.')
            j = -3

        j = j * l**power[i]

    return j
Example #7
0
def tau(n):

    #*****************************************************************************80
    #
    ## TAU returns the value of TAU(N), the number of distinct divisors of N.
    #
    #  Discussion:
    #
    #    TAU(N) is the number of divisors of N, including 1 and N.
    #
    #  First values:
    #
    #     N   TAU(N)
    #
    #     1    1
    #     2    2
    #     3    2
    #     4    3
    #     5    2
    #     6    4
    #     7    2
    #     8    4
    #     9    3
    #    10    4
    #    11    2
    #    12    6
    #    13    2
    #    14    4
    #    15    4
    #    16    5
    #    17    2
    #    18    6
    #    19    2
    #    20    6
    #
    #  Formula:
    #
    #    If the prime factorization of N is
    #
    #      N = P1^E1 * P2^E2 * ... * PM^EM,
    #
    #    then
    #
    #      TAU(N) = ( E1 + 1 ) * ( E2 + 1 ) * ... * ( EM + 1 ).
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    25 February 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer N, the value to be analyzed.  N must be 1 or
    #    greater.
    #
    #    Output, integer TAUN, the value of TAU(N).  But if N is 0 or
    #    less, TAUN is returned as 0, a nonsense value.  If there is
    #    not enough room for factoring, TAUN is returned as -1.
    #
    from i4_factor import i4_factor

    if (n <= 0):
        value = 0
        return value

    if (n == 1):
        value = 1
        return value


#
#  Factor N.
#
    nfactor, factor, power, nleft = i4_factor(n)

    if (nleft != 1):
        print ''
        print 'TAU - Fatal error!'
        print '  Not enough factorization space.'

    value = 1
    for i in range(0, nfactor):
        value = value * (power[i] + 1)

    return value
Example #8
0
def tau ( n ):

#*****************************************************************************80
#
## TAU returns the value of TAU(N), the number of distinct divisors of N.
#
#  Discussion:
#
#    TAU(N) is the number of divisors of N, including 1 and N.
#
#  First values:
#
#     N   TAU(N)
#
#     1    1
#     2    2
#     3    2
#     4    3
#     5    2
#     6    4
#     7    2
#     8    4
#     9    3
#    10    4
#    11    2
#    12    6
#    13    2
#    14    4
#    15    4
#    16    5
#    17    2
#    18    6
#    19    2
#    20    6
#
#  Formula:
#
#    If the prime factorization of N is
#
#      N = P1^E1 * P2^E2 * ... * PM^EM,
#
#    then
#
#      TAU(N) = ( E1 + 1 ) * ( E2 + 1 ) * ... * ( EM + 1 ).
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    25 February 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the value to be analyzed.  N must be 1 or
#    greater.
#
#    Output, integer TAUN, the value of TAU(N).  But if N is 0 or
#    less, TAUN is returned as 0, a nonsense value.  If there is
#    not enough room for factoring, TAUN is returned as -1.
#
  from i4_factor import i4_factor

  if ( n <= 0 ):
    value = 0
    return value

  if ( n == 1 ):
    value = 1
    return value
#
#  Factor N.
#
  nfactor, factor, power, nleft = i4_factor ( n )

  if ( nleft != 1 ):
    print ''
    print 'TAU - Fatal error!'
    print '  Not enough factorization space.'

  value = 1
  for i in range ( 0, nfactor ):
    value = value * ( power[i] + 1 )

  return value
Example #9
0
def moebius ( n ):

#*****************************************************************************80
#
## MOEBIUS returns the value of MU(N), the Moebius function of N.
#
#  Definition:
#
#    MU(N) is defined as follows:
#
#      MU(N) = 1 if N = 1;
#              0 if N is divisible by the square of a prime;
#              (-1)^K, if N is the product of K distinct primes.
#
#  First values:
#
#     N  MU(N)
#
#     1    1
#     2   -1
#     3   -1
#     4    0
#     5   -1
#     6    1
#     7   -1
#     8    0
#     9    0
#    10    1
#    11   -1
#    12    0
#    13   -1
#    14    1
#    15    1
#    16    0
#    17   -1
#    18    0
#    19   -1
#    20    0
#
#    As special cases, MU(N) is -1 if N is a prime, and MU(N) is 0
#    if N is a square, cube, etc.
#
#    The Moebius function is related to Euler's totient function:
#
#      PHI(N) = Sum ( D divides N ) MU(D) * ( N / D ).
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    23 February 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the value to be analyzed.
#
#    Output, integer VALUE, the value of MU(N).
#    If N is less than or equal to 0, MU will be returned as -2.
#    If there was not enough internal space for factoring, MU
#    is returned as -3.
#
  from i4_factor import i4_factor

  if ( n <= 0 ):
    value = -2
    return value

  if ( n == 1 ):
    value = 1
    return value
#
#  Factor N.
#
  nfactor, factor, power, nleft = i4_factor ( n )

  if ( nleft != 1 ):
    print ''
    print 'MOEBIUS - Fatal error!'
    print '  Incomplete factorization.'
    value = -3

  value = 1

  for i in range ( 0, nfactor ):
 
    value = - value

    if ( 1 < power[i] ):
      value = 0
      return value

  return value
Example #10
0
def omega ( n ) :

#*****************************************************************************80
#
## OMEGA returns OMEGA(N), the number of distinct prime divisors of N.
#
#  First values:
#
#     N   OMEGA(N)
#
#     1    1
#     2    1
#     3    1
#     4    1
#     5    1
#     6    2
#     7    1
#     8    1
#     9    1
#    10    2
#    11    1
#    12    2
#    13    1
#    14    2
#    15    2
#    16    1
#    17    1
#    18    2
#    19    1
#    20    2
#
#  Formula:
#
#    If N = 1, then
#
#      OMEGA(N) = 1
#
#    else if the prime factorization of N is
#
#      N = P1^E1 * P2^E2 * ... * PM^EM,
#
#    then
#
#      OMEGA(N) = M
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    24 February 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the value to be analyzed.  N must be 1 or
#    greater.
#
#    Output, integer VALUE, the value of OMEGA(N).  But if N is 0 or
#    less, NDIV is returned as 0, a nonsense value.  If there is
#    not enough room for factoring, NDIV is returned as -1.
#
  from i4_factor import i4_factor

  if ( n <= 0 ):
    value = 0
    return value

  if ( n == 1 ):
    value = 1
    return value
#
#  Factor N.
#
  nfactor, factor, power, nleft = i4_factor ( n )

  if ( nleft != 1 ):
    print ''
    print 'OMEGA - Fatal error!'
    print '  Not enough factorization space.'

  value = nfactor

  return value
Example #11
0
def phi ( n ):

#*****************************************************************************80
#
## PHI computes the number of relatively prime predecessors of an integer.
#
#  Definition:
#
#    PHI(N) is the number of integers between 1 and N which are
#    relatively prime to N.  I and J are relatively prime if they
#    have no common factors.  The function PHI(N) is known as
#    "Euler's totient function".
#
#    By convention, 1 and N are relatively prime.
#
#  First values:
#
#     N  PHI(N)
#
#     1    1
#     2    1
#     3    2
#     4    2
#     5    4
#     6    2
#     7    6
#     8    4
#     9    6
#    10    4
#    11   10
#    12    4
#    13   12
#    14    6
#    15    8
#    16    8
#    17   16
#    18    6
#    19   18
#    20    8
#
#  Formula:
#
#    PHI(U*V) = PHI(U) * PHI(V) if U and V are relatively prime.
#
#    PHI(P^K) = P^(K-1) * ( P - 1 ) if P is prime.
#
#    PHI(N) = N * Product ( P divides N ) ( 1 - 1 / P )
#
#    N = Sum ( D divides N ) PHI(D).
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    25 February 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer N, the value to be analyzed.
#
#    Output, integer VALUE, the value of PHI(N).  If N is less than
#    or equal to 0, PHI will be returned as 0.  If there is not enough
#    room for full factoring of N, PHI will be returned as -1.
#
  from i4_factor import i4_factor

  if ( n <= 0 ):
    value = 0
    return value

  if ( n == 1 ):
    value = 1
    return value
#
#  Factor N.
#
  nfactor, factor, power, nleft = i4_factor ( n )

  if ( nleft != 1 ):
    print ''
    print 'PHI - Fatal error!'
    print '  Not enough factorization space.'
 
  value = 1
  for i in range ( 0, nfactor ):
    value = value * factor[i] ** ( power[i] - 1 ) * ( factor[i] - 1 )

  return value