Advertisement
Guest User

bash script to find primes between range for a given numeric

a guest
Mar 14th, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.17 KB | None | 0 0
  1. #!/bin/bash
  2. #script to find primes between range for a given numeric base
  3. # modified from original from http://phoxis.org
  4. # e.g.
  5. #       ./primes_base_x 1 100 2
  6.  
  7. #Takes an integer as argument, and checks if it is
  8. #a prime number or not. Returns 0 if it is a prime
  9. #or returns 0 otherwise. The integer passed to this
  10. #function should not be a multiple of 2 or 3 or both
  11. function check_prime ()
  12. {
  13.  #initilize vars and counters
  14.   n="$1"
  15.   di=4
  16.   di=$((6-di))
  17.  
  18.   i=5
  19.  
  20.   #check only upto floor (sqrt (n))
  21.   i_lim=$(echo "sqrt ($n)" | bc -l)
  22.   #drop fractional part
  23.   i_lim=${i_lim%.*}
  24.  
  25.   #divide with only numbers not multiple
  26.   #of 2's and 3's and check if n is a prime
  27.   while [ $i -le $i_lim ]
  28.   do
  29.    #check if prime and return 1
  30.    if ((n%i == 0))
  31.    then
  32.     return 1
  33.    fi
  34.    i=$((i+di))
  35.    di=$((6-di))
  36.   done
  37.   #if we are here then n is prime, return 0
  38.   return 0
  39. }
  40.  
  41.  
  42. #Main execusion sequence
  43.  
  44. #check for bounds and initilize limit variables
  45. if [ $# -eq 3 ]
  46.  then
  47.   low=$1
  48.   high=$2
  49.   base=$3
  50.  elif [ $# -eq 1 ]
  51.   then
  52.    low=1
  53.    high=$1
  54.    base=10
  55.  else
  56.   echo "Usage: $0 <low_limit> <high_limit>"
  57.   exit 1
  58. fi
  59.  
  60.  
  61. if [ $low -ge $high ]
  62.  then
  63.   echo "low_limit ($low) exceeds high_limit ($high)"
  64.   echo "Usage: $0 <low_limit> <high_limit>"
  65.   exit 1
  66. fi
  67.  
  68. count=0
  69.  
  70. dx=4
  71. dx=$((6-dx))
  72.  
  73. echo ""
  74.  
  75. #manually print 2 and 3 is needed
  76. if [ $low -le 3 ]
  77.  then
  78.  
  79.   if [ $low -le 2 ]
  80.    then
  81.     count=$((count+1))
  82.    echo "10"
  83.   fi
  84.  
  85.   count=$((count+1))
  86.   echo "11"
  87.  
  88.   low=5
  89. fi
  90.  
  91. #if the lower limit given is a multiple of
  92. #2 or 3, then update it to the next integer
  93. #which is not a multiple of 2's and 3's
  94. while (((low%3 == 0) || (low%2 == 0)))
  95.  do
  96.   low=$((low+1))
  97. done
  98. p=$low
  99.  
  100. #generate integers which are not multiple of
  101. #2's and 3's and pass them to check_prime to
  102. #check for prime, and print
  103. while [ $p -le $high ]
  104.  do
  105.   #call check_prime with p. If 0 returned then
  106.   #p is prime or not a prime otherwise
  107.   check_prime "$p"
  108.   if [ $? -eq 0 ]
  109.   then
  110.    count=$((count+1))
  111.    echo "obase=$base;$p" | bc
  112. #   printf "%5d" "$p"
  113.   fi
  114.  
  115.   p=$((p+dx))
  116.   dx=$((6-dx))
  117. done
  118.  
  119. echo -e "\n\nTotal $count Primes generated\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement