#!/usr/bin/env python3 """The Goldbach comet, docs/comet.svg: for every even N up to 60,000, r(N), the number of ways N = p + q with primes p <= q. A dot's colour is the Hardy-Littlewood factor of N's odd primes, prod (p - 1)/(p - 2), from faint against the background (1, a power of 2) to vivid (4 or more), in light colours or dark as the reader's system is. Its only words are numbers and r(N), so README.md and README.zh.md each caption it. Standard library only: python3 scripts/comet.py > docs/comet.svg """ import math M, W, H = 60000, 720, 340 L, R, T, B = 46, W - 8, 10, H - 26 MARKS = (30030, 32768) LIGHT = ('#7fb3c1', '#4f98aa', '#1f7187', '#03586f', '#0b3a47') DARK = ('#2f6676', '#3f8a9d', '#62b0c2', '#93d2df', '#cdeef5') BINS = 8 spf = list(range(M + 1)) # each number's smallest prime factor for i in range(2, math.isqrt(M) + 1): if spf[i] == i: for j in range(i * i, M + 1, i): if spf[j] == j: spf[j] = i primes = [p for p in range(2, M + 1) if spf[p] == p] r = [0] * (M + 1) for a, p in enumerate(primes): for q in primes[a:]: if p + q > M: break r[p + q] += 1 def factor(n): m = 1.0 while n % 2 == 0: n //= 2 while n > 1: p = spf[n] m *= (p - 1) / (p - 2) while n % p == 0: n //= p return m def ramp(stops, t): f = t * (len(stops) - 1) i = min(len(stops) - 2, int(f)) a, b = (tuple(int(s[k:k + 2], 16) for k in (1, 3, 5)) for s in (stops[i], stops[i + 1])) return '#' + ''.join(f'{round(x + (y - x) * (f - i)):02x}' for x, y in zip(a, b)) top = max(r[4::2]) step = next((s for s in (100, 200, 250, 500, 1000, 2000, 5000) if top / s <= 5), 10000) y_max = math.ceil(top / step) * step X = lambda n: L + n / M * (R - L) Y = lambda c: B - c / y_max * (B - T) num = lambda x: f'{x:.1f}'.rstrip('0').rstrip('.') # One path a colour band, the palest first so the rarer, darker dots lie on top. A dot is a square cap on a pixel # (a zero-length line), each pixel once, each a short move from the one before: some 110 KB for 30,000 N. cells = [set() for _ in range(BINS)] for n in range(4, M + 1, 2): cells[min(BINS - 1, round((factor(n) - 1) / 3 * (BINS - 1)))].add((round(X(n)), round(Y(r[n])))) bands = [] for band in cells: d, at = [], None for x, y in sorted(band): d.append(f'M{x} {y}h0' if at is None else f'm{x - at[0]}{"" if y < at[1] else " "}{y - at[1]}h0') at = (x, y) bands.append(d) css = ['.bg{fill:#fff}.grid{stroke:#ebedf0}.axis{stroke:#dadce0}.t{fill:#5f6368}.ink{fill:#1f1f1f}.ring{stroke:#1f1f1f}.halo{stroke:#fff}'] css += [f'.b{b}{{stroke:{ramp(LIGHT, b / (BINS - 1))}}}' for b in range(BINS)] dark = ['.bg{fill:#1f2023}.grid{stroke:#2e3033}.axis{stroke:#3c4043}.t{fill:#9aa0a6}.ink{fill:#e8eaed}.ring{stroke:#e8eaed}.halo{stroke:#1f2023}'] dark += [f'.b{b}{{stroke:{ramp(DARK, b / (BINS - 1))}}}' for b in range(BINS)] out = [f'', 'The Goldbach comet: r(N) for every even N up to 60,000', f'', f''] for c in range(0, y_max + 1, step): y = round(Y(c)) + 0.5 out.append(f'{c:,}') for n in range(0, M + 1, 10000): out.append(f'{n:,}') out.append(f'r(N)') out += [f'' for b, d in enumerate(bands) if d] for n in MARKS: # a ring, and N with its count beside it x, y = X(n), Y(r[n]) ly = y + 18 if y > T + (B - T) * 0.55 else y - 10 out.append(f'' f'{n:,} ยท {r[n]}') out.append('') print('\n'.join(out))