diff options
| author | cgd <cgd@NetBSD.org> | 1993-04-10 15:51:24 +0000 |
|---|---|---|
| committer | cgd <cgd@NetBSD.org> | 1993-04-10 15:51:24 +0000 |
| commit | 439d2fb46cfec58a16c4a9856cc00ea4a2ee6de3 (patch) | |
| tree | dd34836a974bf8d23a53a2773f34394576710d12 /gnu/usr.bin/bc/Examples/primes.b | |
| parent | a3960efdef82d52889d981e42c8e404f99470203 (diff) | |
gnu bc. necessary to compile the distribution
Diffstat (limited to 'gnu/usr.bin/bc/Examples/primes.b')
| -rw-r--r-- | gnu/usr.bin/bc/Examples/primes.b | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/gnu/usr.bin/bc/Examples/primes.b b/gnu/usr.bin/bc/Examples/primes.b new file mode 100644 index 00000000000..4bef8e8cd69 --- /dev/null +++ b/gnu/usr.bin/bc/Examples/primes.b @@ -0,0 +1,32 @@ + +/* An example that finds all primes between 2 and limit. */ + +define primes (limit) { + auto prime[], num, p, root, i + + prime[1] = 2; + prime[2] = 3; + num = 2; + if (limit >= 2) print "prime 1 = 2\n" + if (limit >= 3) print "prime 2 = 3\n"; + scale = 0; + + for ( p=5; p <= limit; p += 2) { + root = sqrt(p); + isprime = 1; + for ( i = 1; i < num && prime[i] <= root; i++ ) { + if ( p % prime[i] == 0 ) { + isprime = 0; + break; + } + } + if (isprime) { + num += 1; + prime [num] = p; + print "prime ", num, " = ", p, "\n" + } + } +} + + +print "\ntyping 'primes (10)' will print all primes less than 10.\n" |
