For experimental purposes I tried to write a shell script that calculates a whole range of prime numbers. The script seems to be working, but is not very efficient. While writing this script I found a regular expression that does the same thing, only a couple of hundred times faster.
Here is that experimental shell script to calculate a prime number:
#!/bin/sh
throughnoother() {
# A function to calculate if the only argument given is not dividable through any other number.
# Set the status to no. No means, not a prime number. When every test is successful, change
# the status to yes.
status=no
# num starts at 1, and is increased by one each time.
num=1
# This for loop consists of numbers up to half the value of the given argument.
for divider in $(while [ $num lt $(($1/2)) ] ; do num=$(($num+1)) ; echo $num ; done) ; do
# This is not the test to divide through its own value, so skip that.
if [ $1 != $divider ] ; then
# This tests to see if the rounded calculation can be reversed and gives the same result.
if [ $((($1/$divider)*$divider)) = $1 ] ; then
status="yes"
fi
fi
done
# End by printing the status.
echo $status
}
# Start at 2.
number=2
while [ 1 ] ; do
# This first if test, test if the number is dividable through 1 and if it's dividable through itself.
if [ $(($number/1)) = $number a $(($number/$number)) = 1 ] ; then
if [ $(throughnoother $number) != "yes" ] ; then
# If all tests are succesful, then it must be a prime number, print it!
echo "$number"
fi
fi
# Now increase the number to be tested by one.
number=$(($number+1))
done
done
About  Consultancy  Articles  Contact 




References  Red Hat Certified Architect  By Robert de Bock  Robert de Bock 
Curriculum Vitae  By Fred Clausen  +31 6 14 39 58 72  
By Nelson Manning  [email protected] 