Embed a file that isn’t a note, ![[sieve.py]], and it’s shown from the file itself: change the code and the page changes with it.
A code file
![[sieve.py]] is highlighted and titled with its name. Point at it, and its bar shows what you can do with it: wrap its lines, open the raw file, copy the code, or see it on the whole screen. It’s the code that drew the sieve on margin. Its first line is wider than the page: point at it, and the code reaches into the side content to show the rest.
"""The sieve of Eratosthenes: the primes below n, by crossing out multiples."""
def sieve(n: int) -> list[int]:
is_prime = [True] * n
is_prime[0] = is_prime[1] = False
for p in range(2, int(n**0.5) + 1):
if is_prime[p]:
# every multiple of p from p² up is composite
for multiple in range(p * p, n, p):
is_prime[multiple] = False
return [i for i, prime in enumerate(is_prime) if prime]
if __name__ == "__main__":
print(sieve(100))Some of its lines
![[sieve.py#L7-L11]] shows just those lines, numbered from where they start:
for p in range(2, int(n**0.5) + 1):
if is_prime[p]:
# every multiple of p from p² up is composite
for multiple in range(p * p, n, p):
is_prime[multiple] = FalseA code block
Any code block has the same controls, inside it. A block with a title, ```ts title="primes.ts", has them in its bar; one without has them in its top right corner.
// the primes up to n, by crossing out the multiples of each one in turn (Eratosthenes, about 240 BC)
export function primes(n: number): number[] {
const crossed = new Uint8Array(n + 1)
const found: number[] = []
for (let p = 2; p <= n; p++) {
if (crossed[p]) continue
found.push(p)
for (let m = p * p; m <= n; m += p) crossed[m] = 1
}
return found
}
// Euclid's numbers: one more than the product of the first k primes
export const euclid = (k: number) => primes(100).slice(0, k).reduce((a, p) => a * p, 1) + 1basalt dev my-gardenThe same code in two languages can take turns, in tabs.
A table
A csv file, ![[euclid.csv]], becomes a table. These are the Euclid numbers from the proof on margin, and the first six are prime:
| n | primes multiplied | Euclid number | factors |
|---|---|---|---|
| 1 | 2 | 3 | prime |
| 2 | 2 to 3 | 7 | prime |
| 3 | 2 to 5 | 31 | prime |
| 4 | 2 to 7 | 211 | prime |
| 5 | 2 to 11 | 2311 | prime |
| 6 | 2 to 13 | 30031 | 59 × 509 |
| 7 | 2 to 17 | 510511 | 19 × 97 × 277 |
| 8 | 2 to 19 | 9699691 | 347 × 27953 |
| 9 | 2 to 23 | 223092871 | 317 × 703763 |
| 10 | 2 to 29 | 6469693231 | 331 × 571 × 34231 |
A file that isn’t there
A name that matches no file says so, instead of breaking the page: