Other Procedures

Below shows a simple program for the elif command.

> max3 := proc(a, b, c)

> print(`The maximum of`, a, b, c, ` is `);

> if a < b then

> if b < c then c else b fi

> elif a < c then c

> else a

> fi:

> end:

> max3(23,5,50);

There are other useful control operations that can be used in programming. For example, the operation command, op , and number of operations command, nops . The function isprime is a probabilistic primality testing routine.

> Prime_check := proc(A)

> local i, k, prime_cand:

> for i to nops(A) do

> k := op(i,A);

> prime_cand := 2^(2*k+1)-1;

> if isprime(prime_cand)

> then print(prime_cand, ` is prime`);

> else print(prime_cand, ` is not prime`)

> fi;

> od:

> end:

> A := [1,3,5,9,12,21];

A := [1, 3, 5, 9, 12, 21]

> Prime_check(A);

Below is a program for Euclid's algorithm for finding the greatest common divisor of two integers. (Maple has the function igcd that does this automatically.) This demonstrates the while command for programming. It uses the function irem , which computes the integer remainder of m divided by n.

> Euclid_gcd := proc(M,N)

> local a, b, d:

> a := M; b := N;

> while b<>0 do

> d := irem(a,b);

> a := b;

> b := d;

> od:

> a := abs(a):

> lprint(`The integer gcd of`, M, ` and `, N, ` is `, a);

> end:

> Euclid_gcd(-40,16);

`The integer gcd of`, -40, ` and `, 16, ` is `, 8

> igcd(-40,16);