More gawk programs (Dave Robinson, Brisbane, 2004: http://daverobinson.id.au)
BEGIN{w=2 ; x=100
#
This program prints all primes in the range w to x
# (Enter required values of w and x)
for(n=w;n<=x;n++){
for(r=2;r<=sqrt(n);r++){
if(n%r==0){A=1;continue}} # A is a flag
if(A==0){i++; print i," ",n} # Output to screen
A=0}
exit}
**********************************************************************
BEGIN{w=4; x=1000 # This program prints all semiprimes in the range w to x
# (Enter required values of w and x)
for(n=w; n<=x; n++){
for(r=2;r<=sqrt(n);r++){
if(n%r<1){
# n is semiprime if r is prime & n/r
is prime
for(a=2;
a<=sqrt(r); a++){ # Is r prime?
if(r%a<1){A=1; continue}} # A is a flag
if(A<1){
for(b=2; b<=sqrt(n/r); b++){ # Is n/r prime?
if((n/r)%b<1){A=1; continue}}
if(A<1){i++; print i,"
",n >"semiprimes_out.txt"}} # Output to file
A=0}}}
print >"semiprimes_out.txt"
print x," ",i," ",i/x >"semiprimes_out.txt"
exit}
******************************************************************
BEGIN{w=8; x=1000 # This program prints all 3-almost primes in the range w to x
# (Enter required values of w and x)
for(p=w; p<=x; p++){
# PRE-SORT ELEMENT
(can be used to start all similar programs for k=>3)
for(q=2; q<=sqrt(p); q++){
if(p%q<1){n=p/q; break}}
# If p has factors, the first factor
found is a prime
for(r=2;r<=sqrt(n); r++){
if(n%r<1){
# n
is semiprime if r is prime & n/r is prime
for(a=2;
a<=sqrt(r); a++){ # Is r prime?
if(r%a<1){A=1; continue}} # A is a flag
if(A<1){
for(b=2; b<=sqrt(n/r); b++){ # Is n/r prime?
if((n/r)%b<1){A=1; continue}}
if(A<1&&q*n==p){i++; print i," ",p
>"3_almostprimes_out.txt"}} # Output to file
A=0}}}
print >"3_almostprimes_out.txt"
print x," ",i," ",i/x >"3_almostprimes_out.txt"
exit}
******************************************************************
BEGIN{ # Within number ranges beginning with x (>=10) and geometrically stepped by m up to y, find the
# approximate number of numbers which are 1-factor (prime), 2-factor and 3-factor, using formulae
print>>"apformulae_out.txt"
x=100; y=10^16; m=100
#(Enter required values of x, y and m)
for(n=x; n<=y; n=m*n){
f1=n/(log(n)-1)
f2=n*log(log(n))/(log(n)-1)
f3=n*log(log(n))^2/(2*(log(n)-1))
print "n",n,"\t f1=",f1,"\t f2=",f2,"\t f3=",f3 >> "apformulae_out.txt"}
print>>"apformulae_out.txt"
exit}