Iterator - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Mathematics : Discrete Mathematics : Combinatorics : Iterator : Iterator/MixedRadixTuples

Iterator

  

MixedRadixTuples

  

generate mixed-radix tuples

 

Calling Sequence

Parameters

Options

Description

Examples

References

Compatibility

Calling Sequence

MixedRadixTuples(radices, opts)

Parameters

radices

-

list(posint)

opts

-

(optional) equation(s) of the form option = value; specify options for the MixedRadixTuples command

Options

• 

compile = truefalse

  

True means compile the iterator. The default is true.

• 

rank = nonnegint

  

Specify the starting rank of the iterator. The default is one. The starting rank reverts to one when the iterator is reset, reused, or copied.

Description

• 

The MixedRadixTuples command returns an iterator that generates all tuples of a mixed-radix number.

• 

The radices parameter is a list of positive integers that specify the radices of the number. The k-th integer is the radix at the k-th index.

Methods

The iterator object has the following methods. The self parameter is the iterator object.

• 

Number(self): return the number of iterations required to step through the iterator, assuming it started at rank one.

• 

Rank(self,L): return the rank of the current iteration. Optionally pass L, a list or one-dimensional rtable, and return its rank.

• 

Unrank(self,rnk): return a one-dimensional Array corresponding to the iterator output with rank rnk.

Examples

withIterator:

Construct an iterator that generates the tuples with the radices 2,4,3.

MMixedRadixTuples2,4,3:

seqv,v=M

000,100,010,110,020,120,030,130,001,101,011,111,021,121,031,131,002,102,012,112,022,122,032,132

(1)

Compute the number of iterations.

NumberM

24

(2)

Return the element with rank equal to 4.

UnrankM,4

110

(3)

Copy the iterator, but start with rank equal to 4.

NObjectM,rank=4:

seqv,v=N

110,020,120,030,130,001,101,011,111,021,121,031,131,002,102,012,112,022,122,032,132

(4)

Making Change

Given 7 pennies (1-cent coin), 5 nickels (5-cents), 3 dimes (10-cents), and 3 quarters (25-cents), list all ways to change 75 cents.

Assign a procedure that converts a Vector of the number of coins to a string.  The contents of the Vector are the number of pennies, nickels, dimes, and quarters, in that order.

Change := proc(V)
local i;
    sprintf("%d pennies + %d nickels "
            "+ %d dimes + %d quarters "
            "= 75 cents"
            , seq(i, i=V));
end proc:

Assign a predicate that returns true if the Vector V has the exact change.

is_exactVevalbV1+5V2+10V3+25V4=75:

Iterate through all all possibilities, accepting those that are exact, and transforming the Vector to the desired string.

iterMixedRadixTuples7,5,3,3:

forchnginiterdoifis_exactchngthenprintf%s\n,Changechngendifenddo:

5 pennies + 4 nickels + 0 dimes + 2 quarters = 75 cents
5 pennies + 2 nickels + 1 dimes + 2 quarters = 75 cents
0 pennies + 3 nickels + 1 dimes + 2 quarters = 75 cents
5 pennies + 0 nickels + 2 dimes + 2 quarters = 75 cents
0 pennies + 1 nickels + 2 dimes + 2 quarters = 75 cents

References

  

Knuth, Donald Ervin. The Art of Computer Programming, volume 4, fascicle 2; generating all tuples and permutations, p. 2, sec. 7.2.1.1, generating all n-tuples, algorithm M, mixed-radix generation.

Compatibility

• 

The Iterator[MixedRadixTuples] command was introduced in Maple 2016.

• 

For more information on Maple 2016 changes, see Updates in Maple 2016.

See Also

convert[base]

Iterator

Iterator[MixedRadixGrayCode]