Download code

Jump to: navigation, search

Back to Sieve_of_Eratosthenes_(Bash)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

sieve.bash

 1 #!/bin/bash
 2 # The authors of this work have released all rights to it and placed it
 3 # in the public domain under the Creative Commons CC0 1.0 waiver
 4 # (http://creativecommons.org/publicdomain/zero/1.0/).
 5 # 
 6 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 7 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 8 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 9 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
10 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
11 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
12 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 # 
14 # Retrieved from: http://en.literateprograms.org/Sieve_of_Eratosthenes_(Bash)?oldid=19209
15 
16 function mults() { jot - $((2*$1)) $2 $1; }
17 function comps() {
18 	n=$1; shift
19 	for p in $(primes $(dc -e "$n vp")); do
20 		mults $p $n
21 	done | sort -n | uniq
22 }
23 function primes() { [[ $1 -gt 2 ]] && comm -23 <(mults 1 $1) <(comps $1); }
24 
25 primes ${1:-1000} | fmt


hijacker
hijacker
hijacker
hijacker