summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/bc/Examples/primes.b
blob: 2b52ca7e3f8e108e195d02f64b60275ff3b755ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

/* 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;
    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"