# Prime Numbers

This example illustrates the use of vectors, one of LARD's structured data types.

```	use(io).

print_primes_upto_100:expr(void)=(
is_prime:var(bool^101).

clear_multiples(x:val(int)):expr(void)=(
n:var(int).

n:=2*x;
while (n<=100)
do (
is_prime[n]:=false;
n:=n+x
)
).

i:var(int).

forseq i in 1 to 100 do
is_prime[i]:=true;

forseq i in 2 to 100 do
if (is_prime[i])
then (
print(i);
print(" is prime\n");
clear_multiples(i)
)
).

print_primes_upto_100
```
• The function print_primes_upto_100 does all the work. The "main program", at the end, simply calls it.

• The function works by keeping a boolean variable for each number between 1 and 100. These are initialised to true, and then multiples of sucessive numbers are cleared by the sub-function clear_multiples.

• is_prime is the variable used to keep track of what numbers might still be prime. It is of class var(bool^101). bool is the boolean type. ^ is used to declare a vector, giving the base type on the left and the size on the right.

• The elements of a vector are numbered from 0 to n-1.

• Elements are selected using the syntax vector [ index ]. When given a vector variable this can be used as either the left or right hand side of an assignment.

Also note that LARD implements hierarchical scope: the variable n is local to the sub-function clear_multiples, for example. LARD's scope rules are the same as Pascal's.

A natural extension of this example is to write a function that takes as a parameter the upper limit on the numbers to process, for example with this declaration:

```	print_primes_upto(max:val(int)):expr(void)=(...
```
This requires that the declaration of is_prime depends on the parameter max, i.e.

```	  is_prime:var(bool^(max+1)).
```
In general declarations like this are allowed, but it is crucial that the expression's value does not change while the variable remains in scope!

Also note that a bug in the current version of the compiler (see the release notes) makes it necessary to put eval() around the expression, as follows:

```	  is_prime:var(bool^eval(max+1)).
```