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
If You Liked This Post Please Take a Time To Share This Post
pls provide the comments of the program
ReplyDeletei need the commnets for each line of the program
ReplyDeleteWrite a Shell Script to find first n prime numbes
ReplyDeleteThis version works way quicker by only dividing by previous found primes:
ReplyDeleteecho -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
Even faster..
Deleteunset 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
spr
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis 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.
ReplyDeletepython course in pune
python course in chennai
python Training in Bangalore
This looks absolutely perfect. All these tiny details are made with lot of background knowledge. I like it a lot.
ReplyDeleteData Science Training in Indira nagar
Data Science training in marathahalli
Data Science Interview questions and answers
Data Science training in btm layout
Data Science Training in BTM Layout
Data science training in kalyan nagar
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.
ReplyDeleteBest Devops Training in pune
Microsoft azure training in Bangalore
Power bi training in Chennai
I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
ReplyDeleteBest Devops online Training
Online DevOps Certification Course - Gangboard
Thanks For Sharing Your Information Please Keep UpDating Us Time Just Went On Reading The article The Information shared Is Very Helpful
ReplyDeleteDatascience Online Training Aws Online Training Python Online Training Devops Online Training
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.
ReplyDeleteI think this is one of the best blog for me because this is really helpful for me. Thanks for sharing this valuable information
ReplyDeleteinternship meaning | internship meaning in tamil | internship work from home | internship certificate format | internship for students | internship letter | Internship completion certificate | internship program | internship certificate online | internship graphic design
This post is so usefull and informaive keep updating with more information.....
ReplyDeleteFuture Of Data Scientists
Career In Data Science