RCM3720 Cryptography, Network and Computer Security
Laboratory Class 1: Introduction to Axiom
Numbers and arithmetic
- You can treat Axiom like a glorified calculator. Enter the following:
- 3+5
- 5*7
- 2^3/3^5
- (3^4)^5
- 3^(4^5)
- What happens if you enter the last command without the brackets?
- To obtain the factorial n!, use the Axiom command
factorial:
- By trial and error, find the smallest number whose factorial
ends in six zeros.
Lists
- Assignment is done using ":="
where the colon-equals symbols are
used for assigning a particular object to a variable.
- Lists are created using square brackets;
- mylist1:=[k^2 for k in 1..10]
- We can operate on all elements of a list using the
reduce command:
- reduce(+,mylist1)
- reduce(*,mylist2)
- Of course, these could be done as single commands:
- reduce(+,[k^2 for k in 1..10])
- reduce(*,[1/j for j in 5..15])
- Notice how the last result is given as a single large fraction. To
obtain a decimal result we can do either of two things:
- Convert the output to be of type ``Float'':
- reduce(*,[1/j for j in 5..15])::Float
- Two colons can be used to change the type of an object.
- Use floats in the initial command:
- reduce(*,[1.0/j for j in 5..15])
- Using lists, add up the first 1000 integers.
- By trial and error, find the smallest number n for which
the sum of the first n reciprocals is bigger than 8.
- We can also add numbers by using the sum function; here's how
to add the first 100 reciprocals:
Functions and maps
- We shall create a simple function, and apply it to mylist1 from
above:
- f(x) == x-2
- map(f,mylist1)
- Supposing we want to subtract 2 from every element of a list without
having to create a function first. In this case we can use the
"mapping" symbols:
- Create a list called nums containing all the integers from 1
to 100. Now we shall create a simple function f(x) which
returns x if it is prime, and 0 otherwise. The Axiom
function prime? tests for primality:
- f(x)==if prime?(x) then x else 0
- Now apply this function f to nums.
Remove all the zeros:
(% refers to the output of the last command.)
- and determine how many primes there are, using the hash symbol #
which can be used to count the number of elements in a list:
- These last commands can be done as a single command:
- Use the last command to create a function called numprimes
which will count the number of primes below any given integer.
- How many primes are there less than 1000? Less than 10000?
- Alternatively, we can list all the primes below 100 by creating our
list using the "such that" operator---a vertical stroke:
- [k for k in 1..100 | prime?(k)]
- or we could just return the length of the list:
- #[k for k in 1..100 | prime?(k)]
- Use this approach to create a function called numprimes2
which will count the number of primes below any given integer.
- How many primes are there less than 2000? Less than 15000?
Housekeeping
Axiom contains many commands for managing your workspace and your
environment; such commands are all prefixed with a right parenthesis.
- Sometimes you need to clear a variable, say a variable x:
- Most commands of this sort can be abbreviated using their
first two letters:
- To clean out everything:
- To see what variables you've accumulated over your work:
- )display names
- or abbreviated as )d n
- You may have noticed earlier that Axiom poured out lots
of messages when it first "got going". These can be turned off:
- )set messages autoload off
- Note here that if you just type in ")set" or its abbreviation
")se", you'll be presented with the list of all the possible
options. Likewise ")se me" lists all possible options for
messages, and so on.
- Can you find the command which turns on a time function,
so gives the time to compute each command?
- The command ")summary gives a quick summary of these
commands.
- To quit Axiom, type
- or its one letter abbreviation ")q", followed by y to
confirm.