Exemplo n.º 1
0
def comp_random ( n, k, seed ):

#*****************************************************************************80
#
## COMP_RANDOM selects a random composition of the integer N into K parts.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    22 April 2015
#
#  Author:
#
#    John Burkardt
#
#  Reference:
#
#    Albert Nijenhuis Herbert Wilf,
#    Combinatorial Algorithms,
#    Academic Press, 1978, second edition,
#    ISBN 0-12-519260-6.
#
#  Parameters:
#
#    Input, integer N, the integer to be decomposed.
#
#    Input, integer K, the number of parts in the composition.
#
#    Input, integer SEED, a seed for the random number generator.
#
#    Output, integer A(K), the parts of the composition.
#
#    Output, integer SEED, an updated seed for the random number generator.
#
  import numpy as np
  from ksub_random2 import ksub_random2

  b, seed = ksub_random2 ( n + k - 1, k - 1, seed )

  a = np.zeros ( k )
  for i in range ( 0, k - 1 ):
    a[i] = b[i]
  a[k-1] = n + k

  l = 0

  for i in range ( 0, k ):
    m = a[i]
    a[i] = a[i] - l - 1
    l = m

  return a, seed
Exemplo n.º 2
0
def comp_random(n, k, seed):

    #*****************************************************************************80
    #
    ## COMP_RANDOM selects a random composition of the integer N into K parts.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    22 April 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Reference:
    #
    #    Albert Nijenhuis, Herbert Wilf,
    #    Combinatorial Algorithms,
    #    Academic Press, 1978, second edition,
    #    ISBN 0-12-519260-6.
    #
    #  Parameters:
    #
    #    Input, integer N, the integer to be decomposed.
    #
    #    Input, integer K, the number of parts in the composition.
    #
    #    Input, integer SEED, a seed for the random number generator.
    #
    #    Output, integer A(K), the parts of the composition.
    #
    #    Output, integer SEED, an updated seed for the random number generator.
    #
    import numpy as np
    from ksub_random2 import ksub_random2

    b, seed = ksub_random2(n + k - 1, k - 1, seed)

    a = np.zeros(k)
    for i in range(0, k - 1):
        a[i] = b[i]
    a[k - 1] = n + k

    l = 0

    for i in range(0, k):
        m = a[i]
        a[i] = a[i] - l - 1
        l = m

    return a, seed
Exemplo n.º 3
0
def ksub_to_comp_test():

    #*****************************************************************************80
    #
    ## KSUB_TO_COMP_TEST tests KSUB_TO_COMP.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    10 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from ksub_random2 import ksub_random2

    print ''
    print 'KSUB_TO_COMP_TEST'
    print '  KSUB_TO_COMP returns the composition corresponding to a K subset.'

    ns = 14
    ks = 4
    seed = 123456789

    for i in range(0, 5):

        print ''

        bs, seed = ksub_random2(ns, ks, seed)
        print '  KSUB:',
        for j in range(0, ks):
            print '  %2d' % (bs[j]),
        print ''

        nc, kc, ac = ksub_to_comp(ns, ks, bs)
        print '  COMP:',
        for j in range(0, kc):
            print '  %2d' % (ac[j]),
        print ''


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

    return
Exemplo n.º 4
0
def ksub_to_compnz_test():

    # *****************************************************************************80
    #
    ## KSUB_TO_COMPNZ_TEST tests KSUB_TO_COMPNZ.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    24 May 2015
    #
    #  Author:
    #
    #    John Burkardt
    #
    from ksub_random2 import ksub_random2

    print ""
    print "KSUB_TO_COMPNZ_TEST"
    print "  KSUB_TO_COMPNZ returns the nonzero composition"
    print "  corresponding to a K subset."

    ns = 14
    ks = 4
    seed = 123456789

    for i in range(0, 5):

        print ""

        bs, seed = ksub_random2(ns, ks, seed)
        print "  KSUB:  ",
        for j in range(0, ks):
            print "  %2d" % (bs[j]),
        print ""

        nc, kc, ac = ksub_to_compnz(ns, ks, bs)
        print "  COMPNZ:",
        for j in range(0, kc):
            print "  %2d" % (ac[j]),
        print ""

    #  Terminate.
    #
    print ""
    print "KSUB_TO_COMPNZ_TEST:"
    print "  Normal end of execution."

    return
Exemplo n.º 5
0
def ksub_to_comp_test ( ):

#*****************************************************************************80
#
## KSUB_TO_COMP_TEST tests KSUB_TO_COMP.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    10 May 2015
#
#  Author:
#
#    John Burkardt
#
  from ksub_random2 import ksub_random2

  print ''
  print 'KSUB_TO_COMP_TEST'
  print '  KSUB_TO_COMP returns the composition corresponding to a K subset.'

  ns = 14
  ks = 4
  seed = 123456789

  for i in range ( 0, 5 ):

    print ''

    bs, seed = ksub_random2 ( ns, ks, seed )
    print '  KSUB:',
    for j in range ( 0, ks ):
      print '  %2d' % ( bs[j] ),
    print ''

    nc, kc, ac = ksub_to_comp ( ns, ks, bs )
    print '  COMP:',
    for j in range ( 0, kc ):
      print '  %2d' % ( ac[j] ),
    print ''
#
#  Terminate.
#
  print ''
  print 'KSUB_TO_COMP_TEST:'
  print '  Normal end of execution.'

  return
Exemplo n.º 6
0
def compnz_random(n, k, seed):

    #*****************************************************************************80
    #
    ## COMPNZ_RANDOM selects a random composition of the integer N into K nonzero parts.
    #
    #  Licensing:
    #
    #    This code is distributed under the GNU LGPL license.
    #
    #  Modified:
    #
    #    22 December 2014
    #
    #  Author:
    #
    #    John Burkardt
    #
    #  Reference:
    #
    #    Albert Nijenhuis and Herbert Wilf,
    #    Combinatorial Algorithms,
    #    Academic Press, 1978, second edition,
    #    ISBN 0-12-519260-6.
    #
    #  Parameters:
    #
    #    Input, integer N, the integer to be decomposed.
    #
    #    Input, integer K, the number of parts in the composition.
    #    K must be no greater than N.
    #
    #    Input, integer SEED, a seed for the random number generator.
    #
    #    Output, integer A(K), the parts of the composition.
    #
    #    Output, integer SEED, an updated seed for the random number generator.
    #
    import numpy as np
    from ksub_random2 import ksub_random2

    a = np.zeros(k, dtype=np.int32)

    if (1 < n and 1 < k):
        [b, seed] = ksub_random2(n - 1, k - 1, seed)

    for i in range(0, k - 1):
        a[i] = b[i]

    a[k - 1] = n
    l = 0

    for i in range(0, k):
        m = a[i]
        a[i] = a[i] - l - 1
        l = m

    for i in range(0, k):
        a[i] = a[i] + 1

    return a, seed
Exemplo n.º 7
0
def compnz_random ( n, k, seed ):

#*****************************************************************************80
#
## COMPNZ_RANDOM selects a random composition of the integer N into K nonzero parts.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    22 December 2014
#
#  Author:
#
#    John Burkardt
#
#  Reference:
#
#    Albert Nijenhuis and Herbert Wilf,
#    Combinatorial Algorithms,
#    Academic Press, 1978, second edition,
#    ISBN 0-12-519260-6.
#
#  Parameters:
#
#    Input, integer N, the integer to be decomposed.
#
#    Input, integer K, the number of parts in the composition.
#    K must be no greater than N.
#
#    Input, integer SEED, a seed for the random number generator.
#
#    Output, integer A(K), the parts of the composition.
#
#    Output, integer SEED, an updated seed for the random number generator.
#
  import numpy as np
  from ksub_random2 import ksub_random2

  a = np.zeros ( k, dtype = np.int32 )

  if ( 1 < n and 1 < k ):
    [ b, seed ] = ksub_random2 ( n - 1, k - 1, seed )

  for i in range ( 0, k - 1 ):
    a[i] = b[i]

  a[k-1] = n
  l = 0

  for i in range ( 0, k ):
    m = a[i]
    a[i] = a[i] - l - 1
    l = m

  for i in range ( 0, k ):
    a[i] = a[i] + 1

  return a, seed
Exemplo n.º 8
0
def count_pose_random ( seed ):

#*****************************************************************************80
#
## COUNT_POSE_RANDOM poses a problem for the game "The Count is Good"
#
#  Discussion:
#
#    The French television show "The Count is Good" has a game that goes
#    as follows:
#
#      A number is chosen at random between 100 and 999.  This is the GOAL.
#
#      Six numbers are randomly chosen from the set 1, 2, 3, 4, 5, 6, 7, 8,
#      9, 10, 25, 50, 75, 100.  These numbers are the BLOCKS.
#
#      The player must construct a formula, using some or all of the blocks,
#      (but not more than once), and the operations of addition, subtraction,
#      multiplication and division.  Parentheses should be used to remove
#      all ambiguity.  However, it is forbidden to use subtraction in a
#      way that produces a negative result, and all division must come out
#      exactly, with no remainder.
#
#    This routine poses a sample problem from the show.  The point is,
#    to determine how to write a program that can solve such a problem.
#
#  Licensing:
#
#    This code is distributed under the GNU LGPL license.
#
#  Modified:
#
#    06 May 2015
#
#  Author:
#
#    John Burkardt
#
#  Reference:
#
#    Raymond Seroul,
#    Programming for Mathematicians,
#    Springer Verlag, 2000, page 355-357.
#
#  Parameters:
#
#    Input, integer SEED, a seed for the random number generator.
#
#    Output, integer BLOCKS(6), the six numbers available for the formula.
#
#    Output, integer GOAL, the goal number.
#
#    Output, integer SEED, an updated seed for the random number generator.
#
  import numpy as np
  from i4_uniform_ab import i4_uniform_ab
  from ksub_random2 import ksub_random2

  stuff = np.array ( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 75, 100 ] )

  i4_lo = 100
  i4_hi = 999
  goal, seed = i4_uniform_ab ( i4_lo, i4_hi, seed )

  m = 14
  n = 6
  ind, seed = ksub_random2 ( m, n, seed )

  blocks = np.zeros ( 6 )

  for i in range ( 0, 6 ):
    blocks[i] = stuff[ind[i]-1]

  return blocks, goal, seed