File: primes.sh
   1 #!/bin/sh
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright (c) 2026 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the "Software"), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # primes [options...] [count...]
  27 #
  28 # Find the first n prime numbers, emitting each one on its own line, with a
  29 # default of 100 thousand primes when not given a count.
  30 #
  31 # The options are, available both in single and double-dash versions
  32 #
  33 #   -h, -help     show this help message
  34 #   -q, -quiet    don't show starting message to the standard error
  35 
  36 
  37 quiet=0
  38 
  39 while [ $# -gt 0 ]; do
  40     case "$1" in
  41         -h|--h|-help|--help)
  42             awk '/^# +primes /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  43             exit 0
  44         ;;
  45 
  46         --) shift; break ;;
  47 
  48         -q|--q|-quiet|--quiet) quiet=1; shift; continue ;;
  49 
  50         -*)
  51             printf "%s: unsupported option '%s'\n" "$0" "$1" >&2
  52             exit 1
  53         ;;
  54     esac
  55 
  56     break
  57 done
  58 
  59 awk -v quiet="${quiet}" '
  60     BEGIN {
  61         s = ARGV[1]
  62         gsub(/_/, "", s)
  63         n = s + 0
  64 
  65         left = 100 * 1000
  66         if (s ~ /^0+/ || n != 0) {
  67             left = n
  68             delete ARGV[1]
  69         }
  70 
  71         fs = "showing first %d prime numbers\n"
  72         if (!quiet) printf(fs, left) > "/dev/stderr"
  73 
  74         # the first prime is the only even one
  75         if (left > 0) { print 2; left-- }
  76 
  77         # all later primes are odd
  78         for (n = 3.0; left > 0; n += 2.0) if (prime(n)) { print n; left-- }
  79 
  80         exit
  81     }
  82 
  83     # check if a positive odd number given is a prime; uses 2 local vars
  84     function prime(n, max, d) {
  85         # checking up to the square-root is what makes the whole tool
  86         # viable, since the double-loop time-complexity is O(N**1.5),
  87         # instead of O(N**2) with a full inner-loop; no factors of N
  88         # ever exceeed N**0.5
  89         max = sqrt(n)
  90 
  91         for (d = 3.0; d <= max; d += 2.0) if ((n % d) == 0.0) return 0
  92         return 1
  93     }
  94 ' "$@"