Next: Solution
Up: Arrays
Previous: Solution
Using an array constructor and the WHERE statement,
implement the following algorithm for finding prime numbers:
- define a vector, Prime, of size n,
- initialise Prime such that Prime(i) = i for i =
1,n
- set i = 2
- for all , ( ) if
Prime(j) is exactly
divisible by i then set Prime(j) = 0, [hint: use the MOD
(remainder) intrinsic in conjunction with a WHERE statement.]
- increment i,
- if i equals n then exit
- if Prime(i) is zero then goto step 5
- goto step 4
Print out all non-zero entries of the vector (the prime numbers).
Hint: the WHERE statement is an array assignment statement and
not a control construct therefore it cannot contain a PRINT statement.
The PACK intrinsic can accept an array argument and a conformable
MASK and will
return a 1D vector of all the elements of the array where the corresponding
mask elements are .TRUE..
Print*, PACK(Array,Mask)
Go back to Notes
Next: Solution
Up: Arrays
Previous: Solution
©University of Liverpool, 1997
Thu May 29 10:11:26 BST 1997Not for commercial use.