07 October 2011

Finding Prime Number using Shell Script in Linux/Unix

15 comments

Q. How do I find prime number using Shell Script


Ans:

#!/bin/bash
prime_1=0
echo "enter the range"
read n
echo " Primenumber between 1 to $n is:"
echo "1"  
echo "2"
for((i=3;i<=n;))
do
for((j=i-1;j>=2;))
do
if [  `expr $i % $j` -ne 0 ] ; then
prime_1=1
else
prime_1=0
break
fi
j=`expr $j - 1`
done
if [ $prime_1 -eq 1 ] ; then
echo $i
fi
i=`expr $i + 1`
done


Output


finding prime number in bash


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

15 comments:

  1. pls provide the comments of the program

    ReplyDelete
  2. i need the commnets for each line of the program

    ReplyDelete
  3. Write a Shell Script to find first n prime numbes

    ReplyDelete
  4. This version works way quicker by only dividing by previous found primes:

    echo -n "1 "; echo -n "2 "; k=1000; h[$k]=2; for((i=3;i<=1000;i++)); do for j in ${h[@]}; do if [ ! "$j" == "" ]; then if [ `expr $i % $j` -ne 0 ] ; then prime_1=1; else prime_1=0; break; fi; fi; done; if [ $prime_1 -eq 1 ] ; then echo -n "$i "; ((k=$k-1)); h[$k]=$i; fi; done; echo

    ReplyDelete
    Replies
    1. Even faster..

      unset h; echo -n "1 "; echo -n "2 "; k=0; h[$k]=2; for((i=3;i<=1000;i++)); do for j in ${h[@]}; do if [ ! "$j" == "" ]; then if [ `expr $i % $j` -ne 0 ] ; then prime_1=1; else prime_1=0; break; fi; fi; done; if [ $prime_1 -eq 1 ] ; then echo -n "$i "; ((k++)); h[$k]=$i; fi; done; echo

      Delete
  5. This comment has been removed by the author.

    ReplyDelete
  6. This blog is the general information for the feature. You got a good work for these blog.We have a developing our creative content of this mind.Thank you for this blog. This for very interesting and useful.
    python course in pune
    python course in chennai
    python Training in Bangalore

    ReplyDelete
  7. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.
    Best Devops Training in pune
    Microsoft azure training in Bangalore
    Power bi training in Chennai

    ReplyDelete
  8. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    Best Devops online Training
    Online DevOps Certification Course - Gangboard

    ReplyDelete
  9. Thanks For Sharing Your Information Please Keep UpDating Us Time Just Went On Reading The article The Information shared Is Very Helpful
    Datascience Online Training Aws Online Training Python Online Training Devops Online Training

    ReplyDelete
  10. Hey. 1 is not prime. By definition, a prime number has only 2 factors 1 and itself, 1 has only 1 factor itself, so it cannot be prime.

    ReplyDelete
  11. This post is so usefull and informaive keep updating with more information.....
    Future Of Data Scientists
    Career In Data Science

    ReplyDelete