lec3.mws

L-3 MCS 320 Friday 26 August 2005

> restart;

1. Integer and Rational Numbers

> n1 := (3^4)^5;

n1 := 3486784401

> n2 := 3^(4^5);

n2 := 37339184874102004353295975418486658822540977678373400775063693172207904061726525122999368893880397722046876506543147515810872705459216085858135133698280918731419174859426258093880701995195640428...n2 := 37339184874102004353295975418486658822540977678373400775063693172207904061726525122999368893880397722046876506543147515810872705459216085858135133698280918731419174859426258093880701995195640428...n2 := 37339184874102004353295975418486658822540977678373400775063693172207904061726525122999368893880397722046876506543147515810872705459216085858135133698280918731419174859426258093880701995195640428...n2 := 37339184874102004353295975418486658822540977678373400775063693172207904061726525122999368893880397722046876506543147515810872705459216085858135133698280918731419174859426258093880701995195640428...n2 := 37339184874102004353295975418486658822540977678373400775063693172207904061726525122999368893880397722046876506543147515810872705459216085858135133698280918731419174859426258093880701995195640428...

> md := kernelopts(maxdigits);

md := 268435448

> huge := 10^md:

Error, Cannot reallocate memory (old_size=224 new_size=113246220)

> ?maxdigits;

For very large numbers, Maple uses the GNU multiple precision library.  Of course, memory consumption is one of the big problems in computer algebra.

Important numbers are prime numbers:

> n := 2^301-1;

n := 4074071952668972172536891376818756322102936787331872501272280898708762599526673412366794751

> isprime(n);

false

> n1 := nextprime(n);

n1 := 4074071952668972172536891376818756322102936787331872501272280898708762599526673412366794779

> isprime(n1);

true

Factoring a number as a product of primes is very hard.  But detecting whether a number is prime is easier.

> n2 := nextprime(n1);

n2 := 4074071952668972172536891376818756322102936787331872501272280898708762599526673412366794917

> a := n1*n2;

a := 16598062275523971834049631454764644604049784928969747599982629318762611245651632585598828978010230089595448673528347505930794448065510268951067743614388406830663836334901212519338343a := 16598062275523971834049631454764644604049784928969747599982629318762611245651632585598828978010230089595448673528347505930794448065510268951067743614388406830663836334901212519338343

> ifactor(a);

Warning, computation interrupted

> isprime(a);

false

Maple simplifies automatically the rational numbers:

> b := 1234/2480;  # 2 is certainly a divisor

b := 617/1240

> gcd(numer(b),denom(b));

1

The simplification is done by dividing numerator and denominator by the greatest common divisor.

2. Irrational and Floating-Point Numbers

We know already that Maple is case sensitive, so we write Pi and not pi:

> evalf(Pi);

3.141592654

The number of decimal places excludes the leading zeros:

> evalf(Pi/10^80); # scientific notation

0.3141592654e-79

> evalf(Pi/1000);  # writes the leading zeros

0.3141592654e-2

Roots are another application where floating-point and approximate arithmetic is used:

> r := 5^(1/3);

r := 5^(1/3)

> approximate_r := evalf(r);

approximate_r := 1.709975947

> approximate_r^3;

5.000000003

The error on the approximate root is of the magnitude the working precision (which is by default 10 decimal places).

The first way to extend the precision is via evalf:

> r30 := evalf(r,30);

r30 := 1.70997594667669698935310887254

This is just executed locally:

> r30^3;

5.000000003

To make the power work out more accurately, we also need

> evalf[30](r30^3); # other way of 30 decimal places arithmetic

4.99999999999999999999999999997

> evalf(5-%,40);

0.3e-28

Doing this globally inside a worksheet is by assigning to Digits:

> Digits := 20;

Digits := 20

> evalf(r30^3);

5.0000000000000000004

> %-5;

0.4e-18

The third way is to use the File/Preferences.

The evalf is software driven floating-point arithmetic which is typically a magnitude slower than the machine arithmetic.

> Digits := 10;

Digits := 10

> evalhf(r30); # evaluates using the double float

1.70997594667669706

> evalhf(Digits);

14.

> ?UseHardwareFloats;

> UseHardwareFloats := true; # request always hardware floats

UseHardwareFloats := true

> evalf(r); # still displays 10 decimal places

1.709975947

> evalf(r30^3); # also calculates with 10 decimal places ...

5.000000003