Factorials

Calculate factorials over a range

#!/usr/bin/env python3
import sys
import functools
start = int(sys.argv[1])
end = int(sys.argv[2])
for n in range(start, end + 1):
    factorial = 1 if n == 0 else functools.reduce(
        lambda x, y: x * y, range(1, n + 1)
    )
    print(f"{n}! = {factorial}")

Posted in Uncategorized | Leave a comment

3D Cube

#!/usr/bin/python3
import matplotlib.pyplot as plt
import numpy as np
axes = [5, 5, 5]
data = np.ones(axes, dtype=bool)
alpha = 0.9
colors = np.empty(axes + [4], dtype=np.float32)
colors[0] = [1, 0, 0, alpha]  # red
colors[1] = [0, 1, 0, alpha]  # green
colors[2] = [0, 0, 1, alpha]  # blue
colors[3] = [1, 1, 0, alpha]  # yellow
colors[4] = [1, 1, 1, alpha]  # white
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.voxels(data, facecolors=colors, edgecolors='grey')
plt.show()

Posted in Uncategorized | Leave a comment

Wrap

Reformat standard input to any width

#!/usr/bin/python3
import sys
import textwrap
w=int(sys.argv[1])
for line in sys.stdin:
    print(textwrap.fill(line,width=w))

Posted in Uncategorized | Leave a comment

Bumpgen

bumpgen.bas

5 DEF SEG=&HB800
10 SCREEN 2:CLS
15 LINE (0,0)-(639,199),1,BF
20 DIM Y(639):FOR Z=0 TO 639:Y(Z)=999:NEXT Z
25 C=9.800000E-03:B=25:F=9.850000E-03
30 FOR Z=200 TO 0 STEP -3
40 FOR X=0 TO 639:A=A+C:J=Z+SIN(A)*B:IF J>Y(X) THEN 1000
50 Y(X)=J:PSET (X,J),0
1000 NEXT X:B=B*8.999999E-01:C=C+F:NEXT Z

Posted in Uncategorized | Leave a comment

Magic

Generate magic squares for any odd side length with #Python

#!/usr/bin/env python3
import sys
def magic_constant(n):
    return n * (n * n + 1) // 2
def magic_square(n):
    square = [[0] * n for _ in range(n)]
    num = 1
    i, j = 0, n // 2
    while num  1 else 3
square = magic_square(n)
width = len(str(n * n))
print(f"Magic constant for n={n}: {magic_constant(n)}\n")
for row in square:
    print(" ".join(f"{x:>{width}}" for x in row))

Posted in Uncategorized | Leave a comment

Perfect numbers

Perfect numbers

#include 

int main(void)
{
    for (unsigned int p = 2; p <= 23; p++) {
        unsigned long long m = (1ULL << p) - 1;

        int prime = 1;

        for (unsigned long long i = 2; i * i <= m; i++) {
            if (m % i == 0) {
                prime = 0;
                break;
            }
        }

        if (prime)
            printf("%llu\n", (1ULL << (p - 1)) * m);
    }
}

Posted in Uncategorized | Leave a comment

Comb

Combinations of r elements chosen from a pool of n elements with awk

BEGIN {
        for (i=1; i <= r; i++) {
                A[i] = i
                if (i < r ) printf i OFS
                else print i}
        while (A[1] = 1; i--) {
                        if (A[i] < n - r + i) {
                                A[i]++
                                p = i
                                break}}
                for (i = p + 1; i <= r; i++) A[i] = A[i - 1] + 1
                for (i=1; i <= r; i++) {
                        if (i < r) printf A[i] OFS
                        else print A[i]}}
        exit}

$ awk -v r=3 -v n=5 -f comb.awk

Posted in Uncategorized | Leave a comment

Hex

xxd style hex dumper

#!/usr/bin/python3
import sys
with open(sys.argv[1], "rb") as f:
    data = f.read()
for offset in range(0, len(data), 16):
    b = data[offset:offset + 16]
    left = " ".join(f"{x:02x}" for x in b[:8])
    right = " ".join(f"{x:02x}" for x in b[8:])
    hexpart = f"{left:<23}  {right:<23}"
    text = "".join(chr(x) if 32 <= x <= 126 else "." for x in b)
    print(f"{offset:08x}  {hexpart}  |{text}|")

Posted in Uncategorized | Leave a comment

Euler

scale=9
for (e = n = f = 1; f != 0; f /= ++n) e += f
e

Posted in Uncategorized | Leave a comment

Mastodon

Use awk to make your Mastodon follow list into an RSS feed

awk -F'[@,]' '{print "https://" $2 "/@" $1 ".rss"}' following_accounts.csv

Posted in Uncategorized | Leave a comment