primes/primes.sh

43 lines
540 B
Bash
Executable File

#!/bin/sh
# Unix Shell version of a Prime Number Finder
# haplo@mindstab.net
odd=3
sqr=4
max=2
i=3
if [ ! $1 ] ; then
echo "primes.sh [Max number]"
exit
fi
MAX=$1
if [ $MAX -le 0 ] ; then
echo "Max must be a valid number"
exit
fi
while [ $i -lt $MAX ] ;
do
isP=1
x=3
while [ $x -le $max -a $isP != 0 ] ;
do
isP=`expr $i % $x`
x=`expr $x + 2`
done
if [ $isP != 0 ] ;
then
echo "$i"
fi
if [ $i -ge $sqr ] ;
then
max=`expr $max + 1`
odd=`expr $odd + 2`
sqr=`expr $odd + $sqr - 2`
fi
i=`expr $i + 2`;
done