Advertisement
rhcp011235

Untitled

Nov 10th, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.66 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. is_prime() {
  4.     local number=$1
  5.     if ((number <= 1)); then
  6.         echo "false"
  7.         return
  8.     fi
  9.     if ((number <= 3)); then
  10.         echo "true"
  11.         return
  12.     fi
  13.     if ((number % 2 == 0 || number % 3 == 0)); then
  14.         echo "false"
  15.         return
  16.     fi
  17.     local i=5
  18.     while ((i * i <= number)); do
  19.         if ((number % i == 0 || number % (i + 2) == 0)); then
  20.             echo "false"
  21.             return
  22.         fi
  23.         ((i += 6))
  24.     done
  25.     echo "true"
  26. }
  27.  
  28. for i in {1..1000}
  29. do
  30.     if [ "$(is_prime "$i")" == "true" ]; then
  31.         echo "$i is a prime number."
  32.     else
  33.         continue
  34.     fi
  35. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement