Debian Codegraph @

primes2.sh

raw symbols SCIP
1 #!/bin/bash
2 # primes2.sh
3
4 # Generating prime numbers the quick-and-easy way,
5 #+ without resorting to fancy algorithms.
6
7 CEILING=10000 # 1 to 10000
8 PRIME=0
9 E_NOTPRIME=
10
11 is_prime ()
12 {
13 local factors
14 factors=( $(factor $1) ) # Load output of `factor` into array.
15
16 if [ -z "${factors[2]}" ]
17 # Third element of "factors" array:
18 #+ ${factors[2]} is 2nd factor of argument.
19 # If it is blank, then there is no 2nd factor,
20 #+ and the argument is therefore prime.
21 then
22 return $PRIME # 0
23 else
24 return $E_NOTPRIME # null
25 fi
26 }
27
28 echo
29 for n in $(seq $CEILING)
30 do
31 if is_prime $n
32 then
33 printf %5d $n
34 fi # ^ Five positions per number suffices.
35 done # For a higher $CEILING, adjust upward, as necessary.
36
37 echo
38
39 exit