answersLogoWhite

0

The answer is quite easy.We run 2 while loops with their syntaxes as used in unix.One increments number from 3 to given range and other loop checks the divisibility of the number with each number from 2 to the value one less than it.

Below is the shell script:

echo enter a range
read rng
echo 2
j=3
while test $j -le $rng
do
i=2
x=`expr $j - 1`
while test $i -le $x
do
if [ `expr $j % $i` -ne 0 ]
then
i=`expr $i + 1`
else
break
fi
done
if [ $i -eq $j ]
then
echo $j
fi
j=`expr $j + 1`
done


Take care of the spaces and syntaxes.....
Happy programming..!!

Above answer run time is very heigh, because loop checks the divisibility of the number with each number from 2 to the value one less than it. Its a bad idea, for finding prime
numbers its enough to check the divisibility of the number with each number from 2 to the value half of the number.

for example to find 17 is a Prime number or not its enough to check the divisibility of the
number with 2 to 8.

Check bellow link for more elegant script.
http://bashscript.blogspot.com/2009/11/shell-script-to-produce-prime-numbers.html

Venu Madhav Reddy

User Avatar

Wiki User

15y ago

Still curious? Ask our experts.

Chat with our AI personalities

ProfessorProfessor
I will give you the most educated answer.
Chat with Professor
RossRoss
Every question is just a happy little opportunity.
Chat with Ross
DevinDevin
I've poured enough drinks to know that people don't always want advice—they just want to talk.
Chat with Devin

Add your answer:

Earn +20 pts
Q: Unix shell script for generating prime numbers?
Write your answer...
Submit
Still have questions?
magnify glass
imp