NAME

A set of auxiliary functions that are often used in PG problems.

DESCRIPTION

This macro creates the following functions that are available for PG:

step($number)
ceil($number)
floor($number)
max(@listNumbers)
min(@listNumbers)
round($number)
lcm(@listNumbers)
gcf(@listNumbers)
gcd(@listNumbers)
isPrime($number)
reduce($numerator,$denominator)
preformat($scalar, "QuotedString")
random_pairwise_coprime($ar1, $ar2, ... )
random_coprime($ar1, $ar2, ... )
random_subset($n, @set)

step function

Usage: step(x);

returns the step function (or Heaviside function) with jump at x=0. That is when x<0, it returns 0, when x>=0 the function returns 1.

Example:

step(3.14159) returns 1

ceil Function

Usage: ceil(x);

returns the ceiling function of x. This rounds up to the nearest integer.

Examples:

ceil(3.14159) returns 4
ceil(-9.75) return -9

floor Function

Usage: floor(x);

returns the floor function of x. This rounds down to the nearest integer.

Examples:

floor(3.14159) returns 3
floor(-9.75) return -10

max function

Usage: max(@arr);

returns the maximum of the values in the array @arr.

Example

max(1,2,3,4,5,6,7) returns 7

min function

Usage: min(@arr);

returns the minimum of the values in the array @arr.

Example

min(1,2,3,4,5,6,7) returns 1

round function

Usage: round(x);

returns integer nearest x.

Example:

round(3.14159) returns 3

Round function

Usage: Round(x);

returns integer nearest x.

Usage: Round(x,n);

returns the number rounded to n digits.

Example:

Round(1.789,2) returns 1.79

lcm function

Usage: lcm(@arr);

returns the lowest common multiple of the array @arr of integers.

Example:

lcm(3,4,5,6) returns 60.

Note: it checks for an empty array, however doesn't check if the inputs are integers.

gcf function

Usage: gcf(@arr);

returns the greatest common factor of the array @arr of integers.

Example:

gcf(20,30,45) returns 5.

Note: it checks for an empty array, however doesn't check if the inputs are integers.

gcd function

Usage: gcd(@arr);

returns the greatest common divisor of the array @arr of integers.

Example:

gcd(20,30,45) returns 5.

Note: this is just an alias for gcf.

random_coprime function

Usage: random_coprime(array of array_refs);

returns relatively prime integers. The arguments should be references to arrays of integers. This returns an n-tuple of relatively prime integers, each one coming from the corresponding array. Random selection is uniform among all possible tuples that are relatively prime. This does not consider (0,0) to be relatively prime.

This function may return an n-tuple where pairs are not coprime. This returns n-tuples where the largest (in absolute) common factor is 1.

In array context, returns an array. Otherwise, an array ref.

Examples:

random_coprime([1..9],[1..9]) may return (2,9) or (1,1) but not (6,8)
random_coprime([-9..-1,1..9],[1..9],[1..9]) may return (-3,7,4), (-1,1,1), or (-2,2,3) but not (-2,2,4)

Note: in the example above (-2,2,3) is valid because not all three share a factor greater than 1. If you don't want to allow pairs of numbers to have a common factor, see random_pairwise_coprime.

random_pairwise_coprime([-9..-1,1..9],[1..9],[1..9]) may return (-3,7,4) or (-1,1,1) but not (-2,2,3)

WARNING: random_coprime() will use a lot of memory and CPU resources if used with too many/too large arguments. For example, random_coprime([-20..20],[-20..20],[-20..20],[-20..20],[-20..20]) involves processing 41^5 arrays. Consider using random_pairwise_coprime() instead. Or breaking things up like: random_coprime([-20..20],[-20..20]),random_coprime([-20..20],[-20..20],[-20..20])

Note for Problem Authors: one reason for developing this function is to be able to create polynomials that don't have a constant factor. For example, if random_coprime([-9..-1,1..9],[1..9],[1..9]) returns (-5,5,3) then building the quadratic 3x^2+5x-5 doesn't lead to a constant multiple to be factored.

random_pairwise_coprime function

Usage: random_pairwise_coprime($arr);

This is similar to the random_coprime function with the additional constraint that all pairs of numbers are also coprime.

Examples:

random_coprime([-9..-1,1..9],[1..9],[1..9]) may return (-3,7,4), (-1,1,1), or (-2,2,3) but not (-2,2,4)
random_pairwise_coprime([-9..-1,1..9],[1..9],[1..9]) may return (-3,7,4) or (-1,1,1) but not (-2,2,3) or (3,5,6)

isPrime function

Usage: isPrime(n);

returns 1 if n is prime and 0 otherwise.

Example:

isPrime(7) returns 1.
isPrime(8) returns 0

Note: this doesn't check if n is negative.

reduce function

Usage: reduce(num,den);

returns the fraction num/den as an array with first entry as the numerator and second as the denominator.

Example:

reduce(15,20) returns (3,4)

preFormat function

Usage: preFormat($scalar, "quoted string");

returns the string preformatted with the $scalar as 0, 1, or -1. takes a number and fixed object, as in "$a x" and formats

Example:

preformat(-1, "\pi") returns "-\pi"

random_subset function

Usage: random_subset($n, @set);

This function returns a randomly ordered array of $n elements selected from the array @set without replacement. Accepts either an array or an array reference for @set.

Example to choose 3 random elements (both do the same thing):

random_subset(3, 'first', 'second', 'third', 'fourth', 'fifth')
random_subset(3, ['first', 'second', 'third', 'fourth', 'fifth'])