#!/usr/bin/ruby

func binprime(p) {
    p >= 2 || return false;
    range(1, p>>1).each { |i|
        (binomial(p, i) % p) && return false;
    };
    return true;
}

func coef(n, e) {
    (e == 0) && return "#{n}";
    (n == 1) && (n = "");
    (e == 1) ? "#{n}x" : "#{n}x^#{e}";
}

func binpoly(p) {
    join(" ", coef(1, p), p.range.reverse.map {|i|
        join(" ", %w(+ -)[(p-i)&1], coef(binomial(p, i), i));
    }...);
}

say "expansions of (x-1)^p:";
10.range.each { |i| say binpoly(i) };
say "Primes to 80: [#{(2..80).grep { binprime(_) }}]";
