コード例 #1
0
ファイル: nim_sum.py プロジェクト: johannesgerer/jburkardt-py
def nim_sum ( i, j ):

#*****************************************************************************80
#
## NIM_SUM computes the Nim sum of two integers.
#
#  Discussion:
#
#    If K is the Nim sum of I and J, then each bit of K is the exclusive
#    OR of the corresponding bits of I and J.
#
#  Example:
#
#     I     J     K     I base 2    J base 2    K base 2
#   ----  ----  ----  ----------  ----------  ----------
#      0     0     0           0           0           0
#      1     0     1           1           0           1
#      1     1     0           1           1           0
#      2     7     5          10         111         101
#     11    28    23        1011       11100       10111
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    26 May 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer I, J, the integers to be Nim-summed.
#
#    Output, integer K, the Nim sum of I and J.
#
  from ubvec_to_ui4 import ubvec_to_ui4
  from ubvec_xor import ubvec_xor
  from ui4_to_ubvec import ui4_to_ubvec

  nbits = 32

  ivec = ui4_to_ubvec ( i, nbits )
  jvec = ui4_to_ubvec ( j, nbits )

  kvec = ubvec_xor ( nbits, ivec, jvec )

  k = ubvec_to_ui4 ( nbits, kvec )

  return k
コード例 #2
0
ファイル: nim_sum.py プロジェクト: wangwt9907/jburkardt-py
def nim_sum(i, j):

    #*****************************************************************************80
    #
    ## NIM_SUM computes the Nim sum of two integers.
    #
    #  Discussion:
    #
    #    If K is the Nim sum of I and J, then each bit of K is the exclusive
    #    OR of the corresponding bits of I and J.
    #
    #  Example:
    #
    #     I     J     K     I base 2    J base 2    K base 2
    #   ----  ----  ----  ----------  ----------  ----------
    #      0     0     0           0           0           0
    #      1     0     1           1           0           1
    #      1     1     0           1           1           0
    #      2     7     5          10         111         101
    #     11    28    23        1011       11100       10111
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    26 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Parameters:
    #
    #    Input, integer I, J, the integers to be Nim-summed.
    #
    #    Output, integer K, the Nim sum of I and J.
    #
    from ubvec_to_ui4 import ubvec_to_ui4
    from ubvec_xor import ubvec_xor
    from ui4_to_ubvec import ui4_to_ubvec

    nbits = 32

    ivec = ui4_to_ubvec(i, nbits)
    jvec = ui4_to_ubvec(j, nbits)

    kvec = ubvec_xor(nbits, ivec, jvec)

    k = ubvec_to_ui4(nbits, kvec)

    return k
コード例 #3
0
def ubvec_xor_test():

    #*****************************************************************************80
    #
    ## UBVEC_XOR_TEST tests UBVEC_XOR;
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    26 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from i4_uniform_ab import i4_uniform_ab
    from ui4_to_ubvec import ui4_to_ubvec
    from ubvec_to_ui4 import ubvec_to_ui4

    n = 10
    seed = 123456789
    test_num = 10

    print ''
    print 'UBVEC_XOR_TEST'
    print '  UBVEC_XOR exclusive-ors unsigned binary vectors representing'
    print '  unsigned integers;'
    print ''
    print '        I        J        K = I XOR J'
    print ''

    for test in range(0, test_num):
        i, seed = i4_uniform_ab(0, 100, seed)
        j, seed = i4_uniform_ab(0, 100, seed)
        bvec1 = ui4_to_ubvec(i, n)
        bvec2 = ui4_to_ubvec(j, n)
        bvec3 = ubvec_xor(n, bvec1, bvec2)
        k = ubvec_to_ui4(n, bvec3)
        print '  %8d  %8d  %8d' % (i, j, k)


#
#  Terminate.
#
    print ''
    print 'UBVEC_XOR_TEST'
    print '  Normal end of execution.'

    return
コード例 #4
0
def ubvec_xor_test ( ):

#*****************************************************************************80
#
## UBVEC_XOR_TEST tests UBVEC_XOR;
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    26 May 2015
#
#  Author:
#
#    John Burkardt
#
  from i4_uniform_ab import i4_uniform_ab
  from ui4_to_ubvec import ui4_to_ubvec
  from ubvec_to_ui4 import ubvec_to_ui4

  n = 10
  seed = 123456789
  test_num = 10

  print ''
  print 'UBVEC_XOR_TEST'
  print '  UBVEC_XOR exclusive-ors unsigned binary vectors representing'
  print '  unsigned integers;'
  print ''
  print '        I        J        K = I XOR J'
  print ''

  for test in range ( 0, test_num ):
    i, seed = i4_uniform_ab ( 0, 100, seed )
    j, seed = i4_uniform_ab ( 0, 100, seed )
    bvec1 = ui4_to_ubvec ( i, n )
    bvec2 = ui4_to_ubvec ( j, n )
    bvec3 = ubvec_xor ( n, bvec1, bvec2 )
    k = ubvec_to_ui4 ( n, bvec3 )
    print '  %8d  %8d  %8d' % ( i, j, k )
#
#  Terminate.
#
  print ''
  print 'UBVEC_XOR_TEST'
  print '  Normal end of execution.'

  return
コード例 #5
0
def subset_gray_unrank ( rank, n ):

#*****************************************************************************80
#
## SUBSET_GRAY_UNRANK produces a subset of an N set of the given Gray code rank.
#
#  Example:
#
#    N = 4
#
#     Rank     A    
#    -----  -------
#
#        1  0 0 0 0
#        2  1 0 0 0
#        3  1 1 0 0
#        4  0 1 0 0
#        5  0 1 1 0
#        6  1 1 1 0
#        7  1 0 1 0
#        8  0 0 1 0
#        9  0 0 1 1
#       10  1 0 1 1
#       11  1 1 1 1
#       12  0 1 1 1
#       13  0 1 0 1
#       14  1 1 0 1
#       15  1 0 0 1
#       16  0 0 0 1
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license. 
#
#  Modified:
#
#    29 May 2015
#
#  Author:
#
#    John Burkardt
#
#  Parameters:
#
#    Input, integer RANK, the rank of the subset in the Gray code ordering.
#
#    Input, integer N, the order of the total set from which
#    subsets will be drawn.
#
#    Output, integer A(N) A(I) is 1 if element I is in the set,
#    and 0 otherwise.
#
  from gray_unrank2 import gray_unrank2
  from ui4_to_ubvec import ui4_to_ubvec

  gray = gray_unrank2 ( rank-1 )

  a = ui4_to_ubvec ( gray, n )

  return a
コード例 #6
0
def ubvec_to_ui4_test ( ):

#*****************************************************************************80
#
## UBVEC_TO_UI4_TEST tests UBVEC_TO_UI4;
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    26 May 2015
#
#  Author:
#
#    John Burkardt
#
  from ui4_to_ubvec import ui4_to_ubvec

  n = 10

  print ''
  print 'UBVEC_TO_UI4_TEST'
  print '  UBVEC_TO_UI4 converts an unsigned binary vector'
  print '  to an unsigned integer;'
  print ''
  print '  UI4 --> UBVEC  -->  UI4'
  print ''

  for i in range ( 0, 11 ):
    bvec = ui4_to_ubvec ( i, n )
    i2 = ubvec_to_ui4 ( n, bvec )
    print '  %2d  ' % ( i ),
    for i in range ( 0, n ):
      print '%1d' % ( bvec[i] ),
    print '  %2d' % ( i2 )
#
#  Terminate.
#
  print ''
  print 'UBVEC_TO_UI4_TEST'
  print '  Normal end of execution.'

  return
コード例 #7
0
ファイル: nim_sum.py プロジェクト: wangwt9907/jburkardt-py
def nim_sum_test():

    #*****************************************************************************80
    #
    ## NIM_SUM_TEST tests NIM_SUM.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    26 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from i4_uniform_ab import i4_uniform_ab
    from ubvec_print import ubvec_print
    from ui4_to_ubvec import ui4_to_ubvec

    n = 32
    ihi = 1000
    ilo = 0
    ntest = 5

    print ''
    print 'NIM_SUM_TEST'
    print '  NIM_SUM computes the Nim sum of two integers.'
    print ''
    print '    I    J    Nim(I+J)'
    print ''

    seed = 123456789

    for i in range(0, ntest):

        i1, seed = i4_uniform_ab(ilo, ihi, seed)
        i1vec = ui4_to_ubvec(i1, n)

        i2, seed = i4_uniform_ab(ilo, ihi, seed)
        i2vec = ui4_to_ubvec(i2, n)

        i3 = nim_sum(i1, i2)
        i3vec = ui4_to_ubvec(i3, n)

        print ''
        print '  I1, I2, I3 in decimal:'
        print ''
        print '  %3d' % (i1)
        print '  %3d' % (i2)
        print '  %3d' % (i3)
        print ''
        print '  I1, I2, I3 in binary:'
        print ''
        ubvec_print(n, i1vec, '')
        ubvec_print(n, i2vec, '')
        ubvec_print(n, i3vec, '')


#
#  Terminate.
#
    print ''
    print 'NIM_SUM_TEST:'
    print '  Normal end of execution.'

    return
コード例 #8
0
ファイル: nim_sum.py プロジェクト: johannesgerer/jburkardt-py
def nim_sum_test ( ):

#*****************************************************************************80
#
## NIM_SUM_TEST tests NIM_SUM.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    26 May 2015
#
#  Author:
#
#    John Burkardt
#
  from i4_uniform_ab import i4_uniform_ab
  from ubvec_print import ubvec_print
  from ui4_to_ubvec import ui4_to_ubvec

  n = 32
  ihi = 1000
  ilo = 0
  ntest = 5

  print ''
  print 'NIM_SUM_TEST'
  print '  NIM_SUM computes the Nim sum of two integers.'
  print ''
  print '    I    J    Nim(I+J)'
  print ''

  seed = 123456789

  for i in range ( 0, ntest ):

    i1, seed = i4_uniform_ab ( ilo, ihi, seed )
    i1vec = ui4_to_ubvec ( i1, n )

    i2, seed = i4_uniform_ab ( ilo, ihi, seed )
    i2vec = ui4_to_ubvec ( i2, n )

    i3 = nim_sum ( i1, i2 )
    i3vec = ui4_to_ubvec ( i3, n )

    print ''
    print '  I1, I2, I3 in decimal:'
    print ''
    print '  %3d' % ( i1 )
    print '  %3d' % ( i2 )
    print '  %3d' % ( i3 )
    print ''
    print '  I1, I2, I3 in binary:'
    print ''
    ubvec_print ( n, i1vec, '' )
    ubvec_print ( n, i2vec, '' )
    ubvec_print ( n, i3vec, '' )
#
#  Terminate.
#
  print ''
  print 'NIM_SUM_TEST:'
  print '  Normal end of execution.'

  return