Advertisement
cwchen

Find prime number with Bash

May 26th, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.46 KB | None | 0 0
  1. #!/bin/bash
  2. declare -i start end
  3. declare -a array
  4.  
  5. start=$1
  6. end=$2
  7.  
  8. for n in $(seq 1 $end); do
  9.     array[$n]=$n
  10. done
  11.  
  12. array[1]=0
  13.  
  14. for j in $(seq 1 $end); do
  15.     if [ ${array[$j]} -eq 0 ]; then
  16.     continue
  17.     fi
  18.  
  19.     k=$(( $j + $j ))
  20.     while [ $k -le $end ]; do
  21.     array[$k]=0
  22.     k=$(( $k + $j ))
  23.     done
  24. done
  25.  
  26. for j in $(seq 1 $end); do
  27.     if [ ${array[$j]} -lt $start ] || [ ${array[$j]} -eq 0 ]; then
  28.     continue
  29.     fi
  30.     echo ${array[$j]}
  31. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement