Skip to content

Instantly share code, notes, and snippets.

@jgamble
Created October 16, 2019 21:33
Show Gist options
  • Select an option

  • Save jgamble/0d9335e43afc1b5f9ba9eb7968ae3516 to your computer and use it in GitHub Desktop.

Select an option

Save jgamble/0d9335e43afc1b5f9ba9eb7968ae3516 to your computer and use it in GitHub Desktop.
Simply a pair of arrays to create the highest (or lowest) prime that is n bits wide. Entered as C, but written in an array form that should be common across most languages. See comments below.
//
// Offsets for creating the highest prime that fits in n bits; the
// highest prime less than 2**n.
//
// n can range from 1 .. 128. If arrays are zero-based in the language
// you want to use, subtract 1 from n before indexing.
//
// I.e.,
// hp24 = (1 << 24) - hiprimeoffset[24];
//
u16 hiprimeoffset[128] = [
0, 1, 1, 3, 1, 3, 1, 5, 3, 3, 9, 3, 1, 3, 19, 15,
1, 5, 1, 3, 9, 3, 15, 3, 39, 5, 39, 57, 3, 35, 1, 5,
9, 41, 31, 5, 25, 45, 7, 87, 21, 11, 57, 17, 55, 21, 115, 59,
81, 27, 129, 47, 111, 33, 55, 5, 13, 27, 55, 93, 1, 57, 25, 59,
49, 5, 19, 23, 19, 35, 231, 93, 69, 35, 97, 15, 33, 11, 67, 65,
51, 57, 55, 35, 19, 35, 67, 299, 1, 33, 45, 83, 25, 3, 15, 17,
141, 51, 115, 15, 69, 33, 97, 17, 13, 117, 1, 59, 31, 21, 37, 75,
133, 11, 67, 3, 279, 5, 69, 119, 73, 3, 67, 59, 9, 137, 1, 159
];
//
// Offsets for creating the lowest prime that still takes up n bits; the
// first prime greater than 2**n.
//
// n can range from 1 .. 128. If arrays are zero-based in the language
// you want to use, subtract 1 from n before indexing.
//
// I.e.,
// hp24 = (1 << 24) + loprimeoffset[24];
//
u16 loprimeoffset[128] = [
0, 1, 3, 1, 5, 3, 3, 1, 9, 7, 5, 3, 17, 27, 3, 1,
29, 3, 21, 7, 17, 15, 9, 43, 35, 15, 29, 3, 11, 3, 11, 15,
17, 25, 53, 31, 9, 7, 23, 15, 27, 15, 29, 7, 59, 15, 5, 21,
69, 55, 21, 21, 5, 159, 3, 81, 9, 69, 131, 33, 15, 135, 29, 13,
131, 9, 3, 33, 29, 25, 11, 15, 29, 37, 33, 15, 11, 7, 23, 13,
17, 9, 75, 3, 171, 27, 39, 7, 29, 133, 59, 25, 105, 129, 9, 61,
105, 7, 255, 277, 81, 267, 81, 111, 39, 99, 39, 33, 147, 27, 51, 25,
281, 43, 71, 33, 29, 25, 9, 451, 41, 277, 165, 67, 27, 7, 29, 51
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment