~ubuntu-branches/ubuntu/feisty/bc/feisty

« back to all changes in this revision

Viewing changes to Examples/primes.b

  • Committer: Bazaar Package Importer
  • Author(s): Dirk Eddelbuettel
  • Date: 2002-04-13 11:33:49 UTC
  • Revision ID: james.westby@ubuntu.com-20020413113349-hl2r1t730b91ov68
Tags: upstream-1.06
ImportĀ upstreamĀ versionĀ 1.06

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* An example that finds all primes between 2 and limit. */
 
3
 
 
4
define primes (limit) {
 
5
    auto num, p, root, i
 
6
 
 
7
    prime[1] = 2;
 
8
    prime[2] = 3;
 
9
    num = 2;
 
10
    if (limit >= 2) print "prime 1 = 2\n"
 
11
    if (limit >= 3) print "prime 2 = 3\n";
 
12
    scale = 0;
 
13
 
 
14
    for ( p=5; p <= limit; p += 2)  {
 
15
        root = sqrt(p);
 
16
        isprime = 1;
 
17
        for ( i = 1;  i < num && prime[i] <= root; i++ ) {
 
18
            if ( p % prime[i] == 0 ) {
 
19
                isprime = 0;
 
20
                break;
 
21
            }
 
22
        }
 
23
        if (isprime) {
 
24
            num += 1;
 
25
            prime [num] = p;
 
26
            print "prime ", num, " = ", p, "\n"
 
27
        }
 
28
     }
 
29
}
 
30
 
 
31
 
 
32
print "\ntyping 'primes (10)' will print all primes less than 10.\n"