Advertisement
mb6ockatf

primes.sh

Mar 31st, 2024
877
0
130 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.67 KB | Source Code | 0 0
  1. #!/usr/bin/env bash
  2. #
  3. round() {
  4.     echo $(printf %.$2f $(echo "scale=$2;(((10^$2)*$1)+0.5)/(10^$2)" | bc))
  5. }
  6.  
  7. is_prime() {
  8.     n="$1"
  9.     if [[ "$n" -lt 2 ]]; then
  10.         echo "false"
  11.         return 0
  12.     elif [[ "$n" -eq 2 ]]; then
  13.         echo "true"
  14.         return 0
  15.     elif (( "$n" % 2 ==  0 )); then
  16.         echo "false"
  17.         return 0
  18.     fi
  19.     result=true
  20.     search_limit=$(echo "sqrt($n)" | bc)
  21.     for i in $(seq 3 $search_limit 2); do
  22.         echo $i
  23.         if (( $n % $i == 0 )); then
  24.             result=false
  25.             break
  26.         fi
  27.     done
  28.     echo $result
  29. }
  30. is_prime "61"
  31.  
  32. count=1
  33. for ((i=1; i<=200; i++)); do
  34.     r=$(shuf -i 63235-3548569 -n 1)
  35.     result=$(is_prime $r)
  36.     echo "$count  |  $r   | $result"
  37.     count=$((count + 1))
  38. done
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement