| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # sieve.sh (ex68.sh) |
| 3 | |
| 4 | # Sieve of Eratosthenes |
| 5 | # Ancient algorithm for finding prime numbers. |
| 6 | |
| 7 | # This runs a couple of orders of magnitude slower |
| 8 | #+ than the equivalent program written in C. |
| 9 | |
| 10 | LOWER_LIMIT=1 # Starting with 1. |
| 11 | UPPER_LIMIT=1000 # Up to 1000. |
| 12 | # (You may set this higher . . . if you have time on your hands.) |
| 13 | |
| 14 | PRIME=1 |
| 15 | NON_PRIME=0 |
| 16 | |
| 17 | let SPLIT=UPPER_LIMIT/2 |
| 18 | # Optimization: |
| 19 | # Need to test numbers only halfway to upper limit. Why? |
| 20 | |
| 21 | |
| 22 | declare -a Primes |
| 23 | # Primes[] is an array. |
| 24 | |
| 25 | |
| 26 | initializescip-shell shell abs-guide 10-4 initialize(). () |
| 27 | { |
| 28 | # Initialize the array. |
| 29 | |
| 30 | i=$LOWER_LIMIT |
| 31 | until [scip-shell system . . `[`. "$i" -gt "$UPPER_LIMIT" ] |
| 32 | do |
| 33 | Primes[i]=$PRIME |
| 34 | let "i += 1" |
| 35 | done |
| 36 | # Assume all array members guilty (prime) |
| 37 | #+ until proven innocent. |
| 38 | } |
| 39 | |
| 40 | print_primesscip-shell shell abs-guide 10-4 print_primes(). () |
| 41 | { |
| 42 | # Print out the members of the Primes[] array tagged as prime. |
| 43 | |
| 44 | i=$LOWER_LIMIT |
| 45 | |
| 46 | until [scip-shell system . . `[`. "$i" -gt "$UPPER_LIMIT" ] |
| 47 | do |
| 48 | |
| 49 | if [scip-shell system . . `[`. "${Primes[i]}" -eq "$PRIME" ] |
| 50 | then |
| 51 | printfscip-shell system . . printf. "%8d" $i |
| 52 | # 8 spaces per number gives nice, even columns. |
| 53 | fi |
| 54 | |
| 55 | let "i += 1" |
| 56 | |
| 57 | done |
| 58 | |
| 59 | } |
| 60 | |
| 61 | siftscip-shell shell abs-guide 10-4 sift(). () # Sift out the non-primes. |
| 62 | { |
| 63 | |
| 64 | let i=$LOWER_LIMIT+1 |
| 65 | # Let's start with 2. |
| 66 | |
| 67 | until [scip-shell system . . `[`. "$i" -gt "$UPPER_LIMIT" ] |
| 68 | do |
| 69 | |
| 70 | if [scip-shell system . . `[`. "${Primes[i]}" -eq "$PRIME" ] |
| 71 | # Don't bother sieving numbers already sieved (tagged as non-prime). |
| 72 | then |
| 73 | |
| 74 | t=$i |
| 75 | |
| 76 | while [scip-shell system . . `[`. "$t" -le "$UPPER_LIMIT" ] |
| 77 | do |
| 78 | let "t += $i " |
| 79 | Primes[t]=$NON_PRIME |
| 80 | # Tag as non-prime all multiples. |
| 81 | done |
| 82 | |
| 83 | fi |
| 84 | |
| 85 | let "i += 1" |
| 86 | done |
| 87 | |
| 88 | |
| 89 | } |
| 90 | |
| 91 | |
| 92 | # ============================================== |
| 93 | # main () |
| 94 | # Invoke the functions sequentially. |
| 95 | initializescip-shell shell abs-guide 10-4 initialize(). |
| 96 | siftscip-shell shell abs-guide 10-4 sift(). |
| 97 | print_primesscip-shell shell abs-guide 10-4 print_primes(). |
| 98 | # This is what they call structured programming. |
| 99 | # ============================================== |
| 100 | |
| 101 | echoscip-shell system . . echo. |
| 102 | |
| 103 | exit 0 |
| 104 | |
| 105 | |
| 106 | |
| 107 | # -------------------------------------------------------- # |
| 108 | # Code below line will not execute, because of 'exit.' |
| 109 | |
| 110 | # This improved version of the Sieve, by Stephane Chazelas, |
| 111 | #+ executes somewhat faster. |
| 112 | |
| 113 | # Must invoke with command-line argument (limit of primes). |
| 114 | |
| 115 | UPPER_LIMIT=$1 # From command-line. |
| 116 | let SPLIT=UPPER_LIMIT/2 # Halfway to max number. |
| 117 | |
| 118 | Primes=( '' $(sescip-shell system . . seq.q $UPPER_LIMIT) ) |
| 119 | |
| 120 | i=1 |
| 121 | until (( ( i += 1 ) > SPLIT )) # Need check only halfway. |
| 122 | do |
| 123 | if [[ -n ${Primes[i]} ]] |
| 124 | then |
| 125 | t=$i |
| 126 | until (( ( t += i ) > UPPER_LIMIT )) |
| 127 | do |
| 128 | Primes[t]= |
| 129 | done |
| 130 | fi |
| 131 | done |
| 132 | echoscip-shell system . . echo. ${Primes[*]} |
| 133 | |
| 134 | exit $? |