#!/bin/bash
# SCRIPT: primenumbers.sh
# USAGE : ./primenumbers.sh <Range Value>
# or;
# ./ primenumbers.sh <Start Range Value> <End Range Value>
# PURPOSE: Produce prime numbers within a range lmit.
############### DEFINE FUNCTIONS HERE ##############
Usage()
{
echo "********BAD ARGUMENTS**********"
echo "Usage: scriptname <Range Value >"
echo " or "
echo "Usage: scriptname <Start Range Value> <End Range Value>"
exit 1
}
################# ARGUMENTS CHECKING #################
[ $# -gt 2 -o $# -lt 1 ] && Usage
[ $# -eq 1 ] && Bnum=2 && Enum=$1
[ $# -eq 2 ] && Bnum=$1 Enum=$2 && [ $1 -gt $2 ] && Usage
[ $1 -lt 2 ] && Bnum=2
############### MAIN PROGRAM STARTS HERE #############
count=1
while [ $Bnum -le $Enum ]
do
num=$Bnum
Prime="yes"
i=1
#while [ $i -lt $((num/2)) ]
while [ $((i*i)) -lt $((num-1)) ]
do
let i++
if [ $((num%i)) -eq 0 ] # you can also use `expr $num % $i`
then
Prime="no"
break
fi
done
[ "$Prime" = "yes" ] && printf "%5d " $num && let count++
# count is used to print 10 values in a row
[ $count -eq 11 ] && count=1 && echo
let Bnum++
done
echo
OUTPUT:
$ ./primenumbers.sh 1 350
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223 227 229
233 239 241 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349
$ ./primenumbers.sh 1900 2500
1901 1907 1913 1931 1933 1949 1951 1973 1979 1987
1993 1997 1999 2003 2011 2017 2027 2029 2039 2053
2063 2069 2081 2083 2087 2089 2099 2111 2113 2129
2131 2137 2141 2143 2153 2161 2179 2203 2207 2213
2221 2237 2239 2243 2251 2267 2269 2273 2281 2287
2293 2297 2309 2311 2333 2339 2341 2347 2351 2357
2371 2377 2381 2383 2389 2393 2399 2411 2417 2423
2437 2441 2447 2459 2467 2473 2477
$ ./primenumbers.sh 200
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113
127 131 137 139 149 151 157 163 167 173
179 181 191 193 197 199
[root@localhost shell]# ./primenumbers.sh 200 100
********BAD ARGUMENTS**********
Usage: scriptname <Range Value>
or
Usage: scriptname <Start Range Value> <End Range Value>
NOTE:
this script can't find the highest prime number,but it can figure out the highest prime number your system is capable of calculating.
Nice method but not good for beginners
ReplyDeleteChanging the line
ReplyDeletewhile [ $i -lt $((num/2)) ] whith while [ $((i*i)) -lt $((num-1)) ]
the script becomes more "speedy"...
prime3a.sh is very impressive, as you point out for large (>12 digit) numbers it slows down, but with a simple change you can speed that up
ReplyDeletehave a single test divide by two
then in the loop start with 3 and increment i++ two times
so you check only odd numbers; 3,5,7,9... here are the times on a Mac Book Pro 2.4 Ghz, using a 12 digit number, the time dropped 40%
using check for two, then only the odd numbers
>time ./prime3a.sh 128963932541
128963932541 is not a prime number
Since it is divisible by 357131
real 0m20.519s
user 0m18.860s
sys 0m1.655s
>time ./fred.sh 128963932541
128963932541 is not a prime number
Since it is divisible by 357131
real 0m11.984s
user 0m11.043s
sys 0m0.937s
i want an easy one...
ReplyDelete#!/bin/bash
ReplyDeletelownum=$1
hinum=$2
for (( i=$lownum; i<=$hinum; i++ ));
do
if [ $(factor $i |wc -w) -le 2 ]
then
echo -n "$i "
fi
done;
$./primecheck 33 5555
gr8 work....
ReplyDeletehttp://Pr1215.blogspot.in
ReplyDeleteThis is really great, I would not describe it better.
ReplyDelete
ReplyDeleteThis is very educational content and written well for a change. It's nice to see that some people still understand how to write a quality post!
Hyundai Xcent Double Din Player
Tata Nexon Double Din Player
Hyundai Verna
Hypersonic OEM Double Din Player
J3l USB Double Din Player
Double Din DVD Player
car accessories
Hyundai Creta OEM Double Din Player
Hyundai i20 OEM Double Din Player
Hyundai Grand i10 OEM Double Din Player