Integer and rational arithmetic

The Maple programming language allows you to work with integers and rational numbers, and you can perform arithmetic with integers and rational numbers using addition, subtraction, multiplication, division and exponentiation. You can use parentheses, but unlike secondary school, you can only use parentheses. For example, in secondary school, you may have used parentheses, brackets and braces to group expressions such as

$\frac{1}{2} \left\lbrace \left[ \frac{1}{3} \left(1 + \frac{1}{2}\right) + \frac{1}{4}\right] - \frac{5}{3} + \left[\frac{1}{2} - \frac{1}{7}\left(\frac{5}{2} - \frac{1}{2}\right)\right]\right\rbrace$

In Maple, the only option is to use parentheses:

$\frac{1}{2} \left( \left( \frac{1}{3} \left(1 + \frac{1}{2}\right) + \frac{1}{4}\right) - \frac{5}{3} + \frac{1}{7} \left(\frac{1}{2} - \left(\frac{5}{2} - \frac{1}{2}\right)\right)\right)$

To enter an expression of integers or rational numbers, you use the operators +, -, *, /, and ^ together with parentheses. You must ensure that you match all parentheses, and standard order of operations apply.

When you launch Maple, be sure to go into Worksheet Mode and select Tools→Options and then select the Display tab and the first option is Input display:. From the drop-down menu, choose Maple Notation and not 2-D Math Notation. The Output display: option should be left as 2-D Math Notation.

[> 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128;

$255$

[> 2^8 - 1;

$255$

[> 1 + 1/2 + 1/3 + 1/4;

$\frac{25}{12}$

Note that if you are using the command-line version of Maple (cmaple), the output will appear as ASCII art:

> 1 + 1/2 + 1/3 + 1/4;
                              25
                              --
                              12

In this introduction, however, we will always display the output with LaTeX, which is very similar to the output displayed in the graphical user interface.

[> 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64 + 1/128;

$\frac{127}{128}$

[> 1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 + 1/10;

$\frac{7381}{2520}$

[> 1 - 1/2^7;

$\frac{127}{128}$

Now consider the following two examples:

[> 3/2^10;

$\frac{3}{1024}$

[> (3/2)^10;

$\frac{59049}{1024}$

You will note that exponentiation and division obey order of operations, so in the first, 2^10 is evaluated first before 3 is divided by that result.

One important concept to remember is that addition and subtraction are performed from left to right, and similarly for multiplication and division:

[> 1 - 2 + 3;

$2$

[> 1 - (2 + 3);

$-4$

[> 1/2*3/4*5/6*7/8*9/10;

$\frac{63}{256}$

Note that Maple does not allow you to have a unary operator (e.g., + or -) immediately following a binary operator: you must places parentheses around the unary operator:

[> 3 + -4;
Error, `-` unexpected
[> 3^-4;
Error, `-` unexpected
[> 3 + (-4);

$-1$

[> 3^(-4);

$\frac{1}{81}$

Comments

The pound symbol # is used in Maple to allow the user to include description of the command in English. This allows one user or programmer to communicate the significance of an expression to others.

[> 3; # A poor approximation of 'pi'

$3$

[> 22/7; # A better approximation of 'pi'

$\frac{22}{7}$

[> 355/113; # A really good approximation of 'pi'

$\frac{355}{113}$

Numerator and denominator

Given a rational number, we can extract the numerator or the denominator using two functions in Maple: numer(...) and denom(...). A function takes zero or more arguments and returns a result. In this case, these functions take rational numbers. The numerator of an integer is that integer, and the denominator of an integer is always $1$; that is, $n = \frac{n}{1}$.

Maple always simplifies rational numbers to eliminate any common factors before numer(...) or denom(...) are applied:

[> numer( 780/19250 ); # The common factor is 10

$78$

[> denom( 780/19250 );

$1925$

Integer functions and operators

Maple has a number of functions and operators that work with integers. The easiest is the factorial operator, which is written just like you see it in secondary school:

[> 10!;

$3628800$

[> 50!;

$30414093201713378043612608166064768844377641568960512000000000000$

There are also implementations of the greatest-common divisor and least-common multiple functions, which take an arbitrary number of arguments. These are implemented as the funtions igcd(...) and ilcm(...):

[> igcd( 210, 280, 924 );

$14$

[> igcd( 210/14, 280/14, 924/14 );

$1$

[> ilcm( 210, 280, 924 );

$9240$

[> ilcm( 16380, 92950 ); 16380*92950/igcd( 16380, 92950 );

$11711700$

The modulus or remainder operator in Maple is mod, and this is an operator, not a function. The right-hand side must be a positive integer, and the result is the remainder when divided by that integer (a number between 0 and the right-hand side minus one:

[> 32525 mod 215;

$60$

The binomial(...) function calculates the binomial coefficients. The binomial coefficients are those numbers that appear as the coefficients of the expansion of $(x + 1)^n$ where binomial( n, k ) is the coefficient of $x^k$ in this expansion. You may have also come across this in your combinatorics class where you learned $n$ choose $k$.

[> binomial( 42, 17 );

$254661927156$

[> binomial( 42, 42 - 17 );

$254661927156$

[> 42!/17!/(42 - 17)!;

$254661927156$

The max(...) and min(...) functions take zero or more arguments and returns the maximum and minimum of these entries:

[> max( 3/13, 59/69, 21/20, 8/5, 65/23, 46/57, 3/29, 2/9, 73/44, 1/24 );

$\frac{65}{23}$

[> min( 3/13, 59/69, 21/20, 8/5, 65/23, 46/57, 3/29, 2/9, 73/44, 1/24 );

$\frac{1}{24}$

The absolute-value function, given a rational $r$, returns $|r|$:

[> abs( -153/23 );

$\frac{153}{23}$

There are integer logarithm functions, so ilog( n ) returns that integer $r$ such that $e^r \le n < e^{r + 1}$, ilog2( n ) returns that integer $r$ such that $2^r \le n < 2^{r + 1}$, and ilog10( n ) returns that integer $r$ such that $10^r \le n < 10^{r + 1}$:

[> ilog( 12345678901234567890 );

$43$

and $e^{43} \approx 4727839468229346561.5$ and $e^{44} \approx 12851600114359308275.8$. Also:

[> ilog2( 12345678901234567890 );

$63$

[> 2^63;

$9223372036854775808$

[> 2^64;

$18446744073709551616$

[> ilog10( 12345678901234567890 );

$19$

[> 10^19;

$10000000000000000000$

Long division: quotient and remainder

When you divide one integer by another using long division, you get a quotient and a remainder. For example, in dividing $n$ by $m$, you get a quotient $q$ and a remainder $r$ such that $n = mq + r$ where $0 \le r < m$, or also $\frac{n}{q} = m + \frac{r}{q}$ where $0 \le \frac{r}{q} < 1$:

[> q := iquo( 525223143132143, 13053 );

$q := 40237734094$

[> r := irem( 525223143132143, 13053 );

$r := 3161$

[> 13053*q + r;

$525223143132143$