summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/bc/Examples/twins.b
diff options
context:
space:
mode:
authorTheo de Raadt <deraadt@cvs.openbsd.org>1995-10-18 08:53:40 +0000
committerTheo de Raadt <deraadt@cvs.openbsd.org>1995-10-18 08:53:40 +0000
commitd6583bb2a13f329cf0332ef2570eb8bb8fc0e39c (patch)
treeece253b876159b39c620e62b6c9b1174642e070e /gnu/usr.bin/bc/Examples/twins.b
initial import of NetBSD tree
Diffstat (limited to 'gnu/usr.bin/bc/Examples/twins.b')
-rw-r--r--gnu/usr.bin/bc/Examples/twins.b40
1 files changed, 40 insertions, 0 deletions
diff --git a/gnu/usr.bin/bc/Examples/twins.b b/gnu/usr.bin/bc/Examples/twins.b
new file mode 100644
index 00000000000..de910a79312
--- /dev/null
+++ b/gnu/usr.bin/bc/Examples/twins.b
@@ -0,0 +1,40 @@
+
+/* An example that finds all primes between 2 and limit. */
+
+define primes (limit) {
+ auto num, p, root, i
+
+ prime[1] = 2;
+ prime[2] = 3;
+ num = 2;
+ 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 "\ntyping 'twins (10)' will print all twin primes less than 10.\n"
+
+define twins (limit) {
+ auto i;
+
+ i = primes(limit+2);
+
+ for (i=1; prime[i] > 0; i++) {
+ if ((prime[i]+2) == prime[i+1]) \
+ print "twins are ", prime[i], " and ", prime[i+1], "\n"
+ }
+}