awk — Number Theory
GCD via Euclidean algorithm
echo "48 18" | awk '{
a=($1>$2)?$1:$2; b=($1>$2)?$2:$1
while(b){t=b; b=a%b; a=t}
printf "gcd(%d,%d) = %d\n", $1, $2, a
}'
GCD of multiple values — pipeline from heredoc
awk 'function gcd(a,b){while(b){t=b;b=a%b;a=t}return a}
NR==1{result=$1} NR>1{result=gcd(result,$1)}
END{printf "gcd = %d\n", result}' <<'EOF'
120
84
36
EOF
LCM from GCD — least common multiple
echo "12 18" | awk '{
a=$1; b=$2; p=a*b
while(b){t=b; b=a%b; a=t}
printf "lcm(%d,%d) = %d\n", $1, $2, p/a
}'
Fibonacci sequence — first N terms
echo "15" | awk '{
n=$1; a=0; b=1
for(i=1;i<=n;i++){
printf "%d ", a
t=a+b; a=b; b=t
}
print ""
}'
# Output: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Prime number test
echo "97" | awk '{
n=$1; prime=1
if(n<2) prime=0
for(i=2;i*i<=n;i++){
if(n%i==0){prime=0; break}
}
printf "%d is %s\n", n, (prime?"prime":"not prime")
}'
Generate primes up to N — sieve of Eratosthenes
echo "100" | awk '{
n=$1
for(i=2;i<=n;i++) s[i]=1
for(i=2;i*i<=n;i++){
if(s[i]) for(j=i*i;j<=n;j+=i) s[j]=0
}
for(i=2;i<=n;i++) if(s[i]) printf "%d ", i
print ""
}'
Factorial calculation
echo "12" | awk '{
n=$1; f=1
for(i=2;i<=n;i++) f*=i
printf "%d! = %d\n", n, f
}'
# Output: 12! = 479001600
Decimal to binary conversion
echo "255" | awk '{
n=$1; bits=""
if(n==0) bits="0"
while(n>0){bits=(n%2)bits; n=int(n/2)}
printf "decimal %d = binary %s\n", $1, bits
}'
Binary to decimal conversion
echo "11111111" | awk '{
n=0; len=length($1)
for(i=1;i<=len;i++){
bit=substr($1,i,1)
n=n*2+bit
}
printf "binary %s = decimal %d\n", $1, n
}'
Decimal to binary and back — round-trip verification
awk 'BEGIN{
for(d=0;d<=16;d++){
n=d; bits=""
if(n==0) bits="0"
while(n>0){bits=(n%2)bits; n=int(n/2)}
# Convert back
dec=0; len=length(bits)
for(i=1;i<=len;i++) dec=dec*2+substr(bits,i,1)
printf "%3d -> %-8s -> %3d %s\n", d, bits, dec, (d==dec?"OK":"FAIL")
}
}'
Percentage change — old vs new value
awk '{
pct=($2-$1)/$1*100
printf "%s -> %s: %+.2f%%\n", $1, $2, pct
}' <<'EOF'
100 125
500 475
1024 2048
80 80
EOF
Compound interest — principal, rate, years
awk '{
principal=$1; rate=$2/100; years=$3
result=principal * (1+rate)^years
printf "$%.2f at %.1f%% for %d years = $%.2f (gain: $%.2f)\n",
principal, $2, years, result, result-principal
}' <<'EOF'
10000 5.0 10
25000 7.5 30
1000 3.2 5
EOF
Doubling time via rule of 72
awk '{
rate=$1
years=72/rate
printf "At %.1f%% annual return: doubles in ~%.1f years\n", rate, years
}' <<'EOF'
3.0
5.0
7.0
10.0
EOF
Moving average over a data stream — window of 3
awk '{
val[NR]=$1; sum+=$1
if(NR>=3){
avg=(val[NR]+val[NR-1]+val[NR-2])/3
if(NR>3) sum_old=(val[NR-1]+val[NR-2]+val[NR-3])/3
printf "point %2d: value=%6.1f 3-pt avg=%6.2f\n", NR, $1, avg
} else {
printf "point %2d: value=%6.1f (warming up)\n", NR, $1
}
}' <<'EOF'
10.0
12.5
11.0
14.0
13.5
16.0
15.5
18.0
17.0
20.0
EOF
2x2 matrix multiply
awk 'BEGIN{
# Matrix A Matrix B
a11=1; a12=2; b11=5; b12=6
a21=3; a22=4; b21=7; b22=8
# C = A * B
c11=a11*b11+a12*b21; c12=a11*b12+a12*b22
c21=a21*b11+a22*b21; c22=a21*b12+a22*b22
printf "A = |%d %d| B = |%d %d|\n", a11, a12, b11, b12
printf " |%d %d| |%d %d|\n", a21, a22, b21, b22
printf "\n"
printf "A*B = |%d %d|\n", c11, c12
printf " |%d %d|\n", c21, c22
}'
# Output:
# A = |1 2| B = |5 6|
# |3 4| |7 8|
#
# A*B = |19 22|
# |43 50|
2D rotation matrix — rotate point (x,y) by angle
awk 'BEGIN{
pi=atan2(0,-1)
# Rotate point (3,4) by 90 degrees
x=3; y=4; deg=90
rad=deg*pi/180
rx=x*cos(rad)-y*sin(rad)
ry=x*sin(rad)+y*cos(rad)
printf "(%g,%g) rotated %d° = (%.4f, %.4f)\n", x, y, deg, rx, ry
}'
# Output: (3,4) rotated 90° = (-4.0000, 3.0000)