Created
April 14, 2024 13:38
-
-
Save paniq/62d7041de3e200848d11bc690ae08484 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# optimally encoding a set of two integers | |
N := 32 | |
N/2 := N // 2 | |
NUMVALS := N * (N + 1) // 2 | |
fn trienc (x y) | |
assert (x < N) | |
assert (y < N) | |
assert (x >= y) | |
x y := if (y >= N/2) | |
pass | |
y - N/2 | |
x - N/2 | |
else | |
pass (x + 1) y | |
y * (N + 1) + x | |
fn tridec (i) | |
assert (i < NUMVALS) | |
y := i // (N + 1) | |
x := i - y * (N + 1) | |
if (y >= x) | |
pass | |
y + N/2 | |
x + N/2 | |
else | |
pass (x - 1) y | |
# optimally encoding a set of two integers, assuming x != y we save one bit. | |
NUMVALS := N * (N - 1) // 2 | |
fn triencne (x y) | |
assert (x < N) | |
assert (y < N) | |
assert (x > y) | |
x y := if (y >= N/2) | |
pass | |
y - N/2 | |
x - N/2 | |
else | |
pass (x - 1) y | |
y * (N - 1) + x | |
fn tridecne (i) | |
assert (i < NUMVALS) | |
y := i // (N - 1) | |
x := i - y * (N - 1) | |
if (y > x) | |
pass | |
y + N/2 | |
x + N/2 | |
else | |
pass (x + 1) y |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment