#!/bin/bash
## Find out all primes below N (inclusive). 
## date: Feb. 02, 2016.
## author: Xiaoxu Guan at LSU HPC.


echo

N=$1


if [[ $N -lt 0 ]] ;  then
   echo "Oops, N should be > 0 !"
   echo "N = $N"
   echo "exit 10"
   exit 10
fi



# note the pitfall of in {2..$N} 

count=0

for i in $(eval echo {2..$N})  # outer loop is level 2.
do
        let imax=$i-1
    for j in $(eval echo {2..$imax})   # inner loop is level 1.
    do
        if [[ i%j -eq 0 ]]; then
              continue 2
        fi
    done
        let "count += 1"   # increment by 1.
        echo "$i is a prime."
done

echo "total number of the primes ="  $count
echo
