• Welcome to Bashguru

    Linux is one of popular version of UNIX operating System. It is open source as its source code is freely available. It is free to use. Linux was designed considering UNIX compatibility. It's functionality list is quite similar to that of UNIX and become very popular over the last several years. Our Basic motive is to provide latest information about Linux Operating system.

  • Python Programming

    Python is a comparatively simple programming language, compared to c++. Although some of the benefits of c++ are abstracted away in python, they are replaced with an overall easier to learn language with many “intuitive” features. For this reason it is common and recommended by most professionals that people new to programming start with python.

  • Perl Programming

    Perl is an open-source, general-purpose interpreted programming language. Used often for CGI, Perl is also used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications. The Perl languages borrow features from other programming languages including C, shell scripting (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary UNIX command line tools, facilitating easy manipulation of text files.

  • Android

    Android is an operating system based on the Linux kernel, and designed primarily for touch screen mobile devices such as smart phones and tablet computers. Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating developed by Google and use it in their mobile devices.Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.

Tuesday, November 17, 2009

Posted by venu k
9 comments | 11:58 AM

#!/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.

9 comments:

  1. Nice method but not good for beginners

    ReplyDelete
  2. Changing the line

    while [ $i -lt $((num/2)) ] whith while [ $((i*i)) -lt $((num-1)) ]

    the script becomes more "speedy"...

    ReplyDelete
  3. 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
    have 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

    ReplyDelete
  4. i want an easy one...

    ReplyDelete
  5. #!/bin/bash

    lownum=$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

    ReplyDelete
  6. This is really great, I would not describe it better.

    ReplyDelete