1 2/* An example that finds all primes between 2 and limit. */ 3 4define 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 32print "\ntyping 'primes (10)' will print all primes less than 10.\n" 33