"Fossies" - the Fresh Open Source Software Archive

Member "bc-1.06.95/Examples/twins.b" (7 Jul 2000, 710 Bytes) of package /linux/misc/old/bc-1.06.95.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Limbo source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    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     scale = 0;
   11 
   12     for ( p=5; p <= limit; p += 2)  {
   13     root = sqrt(p);
   14     isprime = 1;
   15     for ( i = 1;  i < num && prime[i] <= root; i++ ) {
   16         if ( p % prime[i] == 0 ) {
   17         isprime = 0;
   18         break;
   19             }
   20     }
   21     if (isprime) {
   22         num += 1;
   23         prime [num] = p;
   24     }
   25      }
   26 }
   27 
   28 
   29 print "\ntyping 'twins (10)' will print all twin primes less than 10.\n"
   30 
   31 define twins (limit) {
   32    auto i;
   33 
   34    i = primes(limit+2);
   35 
   36    for (i=1; prime[i] > 0; i++) {
   37       if ((prime[i]+2) == prime[i+1]) \
   38     print "twins are ", prime[i], " and ", prime[i+1], "\n"
   39    }
   40 }