#! /bin/sh # primes -- computes prime numbers # $Id: primes,v 1.2 1998/07/09 02:41:10 cdua Exp cdua $ # Carlos Duarte, 971116/980709 # usage: ./primes stop - from 1 to stop # or: ./primes start stop - from start to stop case $# in 1) stop=$1 ;; 2) start=$1; stop=$2 ;; *) echo usage: $0 start stop echo usage: $0 stop exit 1 ;; esac : ${start=1} bc<l) { if (p(l)) l += 0; /* to p(l) do not print */ } /* use this, if input might be random if (a==l) { if (l%2==1) l += 1; l += 1; } */ /* or this, is input is known to always be odd */ l += 2; s = sqrt(a); for (i=0; i s) break; if (a%x[i] == 0) return (0); } x[z++] = a; return (1); } /* check if a is prime, simple algorithm */ define q (a) { s = sqrt(a); for (i=2; i<=s; i++) if (a%i == 0) return (0); return (1); } scale=0 a=$start if (a<2) a=2 while (a<4) { a++ } if (a%2 == 0) a += 1; while (a <= $stop) { if (p(a)) a a += 2; } quit eof exit