codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#!/usr/bin/env bash USAGE="$(basename $0) <options> \n \n \t Print all prime numbers between the two specified numbers. \n \n \t Options \n \n \t\t [-h|--help]\t\t\t This message gets printed only. \n \t\t [-n|--min] <the start number>\t The start number, inclusive. \n \t\t [-m|--max] <the end number>\t The end number, inclusive. \n " args=`getopt -o "hn:m:" -l "help,min,max" -- "$@"` eval set -- "$args" while true; do case "$1" in -h | --help) echo -e ${USAGE}; exit;; -n | --min) shift; min="$1"; shift;; -m | --max) shift; max="$1"; shift;; --) shift; break;; *) echo "Internal error!"; exit 1;; esac done if [[ ${min}%2 -eq 0 ]]; then min=$((${min} + 1)) fi total=0 first_prime=2 second_prime=3 for ((i=${min}; i<=${max};)); do sqrt_of_i=$(echo "sqrt($i)" | bc -l) sqrt_of_i=${sqrt_of_i%.*} is_prime=true for ((j=${second_prime}; j<=${sqrt_of_i};)); do if [[ ${i}%$j -eq 0 && $i -ne $j ]]; then #echo prime=false i=$i j=$j is_prime=false break fi j=$(($j + ${first_prime})) done if [[ $is_prime == true ]]; then #echo is_prime=$is_prime total=$(($total+1)) #echo $i fi i=$(($i + ${first_prime})) done echo "Total primes: $total"
Private
[
?
]
Run code
Submit