cross(pa, pb, po) = { return ((pa[1] - po[1]) * (pb[2] - po[2]) - (pa[2] - po[2]) * (pb[1] - po[1])); } convexHull(points) = { if (#points < 2, return (points), my (lower=[]); for (i=1, #points, while (#lower >= 2 && cross(lower[#lower - 1], lower[#lower], points[i]) <= 0, lower = lower[1..#lower-1]; ); lower = concat(lower, [points[i]]); ); my (upper=[]); forstep (i=#points, 1, -1, while (#upper >= 2 && cross(upper[#upper - 1], upper[#upper], points[i]) <= 0, upper = upper[1..#upper-1]; ); upper = concat(upper, [points[i]]); ); return (concat(lower[1..#lower-1], upper[1..#upper-1])); ); } a(n, base=10) = { my (d=digits(n, base), p=vector(#d, k, [k,d[k]]), h=convexHull(p)); sum(i=2, #h-1, cross(h[i], h[i+1], h[1])); }