Last active
July 27, 2023 21:38
-
-
Save jcalz/df145b327dae9a0f781757b242c1b9f9 to your computer and use it in GitHub Desktop.
Regexes where it is not known whether they match anything
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
// I claim that the following regular expression should match a string if and only if | |
// it is composed of only the character "x", and | |
// its length is an even number greater than two which cannot be expressed as the sum of two prime numbers | |
// that is, if the length of the string is a counterexample to Goldbach's conjecture. | |
const r = /^(?!^(x*(?<!^(?:x?|\2+(xx+))))((?!(?:x?|(xx+?)\4+)$)x*)$)xx(xx)+$/; | |
console.log("start"); | |
for (let i = 0; i < 1000; i++) { | |
const s = "x".repeat(i); | |
if (r.test(s)) console.log(i) | |
} | |
console.log("done"); | |
// well, that proves it doesn't match any lengths less than 1000. | |
// Of course any counterexample to Goldbach's conjecture would be much larger than the maximum possible string | |
// length representable in JavaScript, so I guess we can be sure that r matches no possible inputs | |
// --------------------- | |
// In case you doubt the above claim, this related regular expression *matches* all even numbers that can be represented as the sum of | |
// two primes, and we can use the capture groups to show the primes | |
const goldbachsRegex = /^(?=^(x*(?<!^(?:x?|\2+(xx+))))((?!(?:x?|(xx+?)\4+)$)x*)$)xx(xx)+$/ | |
const results: string[] = []; | |
for (let i = 0; i < 500; i++) { | |
const s = "x".repeat(i).match(goldbachsRegex); | |
if (s) results.push(`${s[0].length}=${s[1].length}+${s[3].length}`); | |
} | |
console.log(results.join("; ")); | |
/* | |
4=2+2; 6=3+3; 8=5+3; 10=7+3; 12=7+5; 14=11+3; 16=13+3; 18=13+5; 20=17+3; 22=19+3; 24=19+5; | |
26=23+3; 28=23+5; 30=23+7; 32=29+3; 34=31+3; 36=31+5; 38=31+7; 40=37+3; 42=37+5; 44=41+3; | |
46=43+3; 48=43+5; 50=47+3; 52=47+5; 54=47+7; 56=53+3; 58=53+5; 60=53+7; 62=59+3; 64=61+3; | |
66=61+5; 68=61+7; 70=67+3; 72=67+5; 74=71+3; 76=73+3; 78=73+5; 80=73+7; 82=79+3; 84=79+5; | |
86=83+3; 88=83+5; 90=83+7; 92=89+3; 94=89+5; 96=89+7; 98=79+19; 100=97+3; 102=97+5; | |
104=101+3; 106=103+3; 108=103+5; 110=107+3; 112=109+3; 114=109+5; 116=113+3; 118=113+5; | |
120=113+7; 122=109+13; 124=113+11; 126=113+13; 128=109+19; 130=127+3; 132=127+5; 134=131+3; | |
136=131+5; 138=131+7; 140=137+3; 142=139+3; 144=139+5; 146=139+7; 148=137+11; 150=139+11; | |
152=149+3; 154=151+3; 156=151+5; 158=151+7; 160=157+3; 162=157+5; 164=157+7; 166=163+3; | |
168=163+5; 170=167+3; 172=167+5; 174=167+7; 176=173+3; 178=173+5; 180=173+7; 182=179+3; | |
184=181+3; 186=181+5; 188=181+7; 190=179+11; 192=181+11; 194=191+3; 196=193+3; 198=193+5; | |
200=197+3; 202=199+3; 204=199+5; 206=199+7; 208=197+11; 210=199+11; 212=199+13; 214=211+3; | |
216=211+5; 218=211+7; 220=197+23; 222=211+11; 224=211+13; 226=223+3; 228=223+5; 230=227+3; | |
232=229+3; 234=229+5; 236=233+3; 238=233+5; 240=233+7; 242=239+3; 244=241+3; 246=241+5; | |
248=241+7; 250=239+11; 252=241+11; 254=251+3; 256=251+5; 258=251+7; 260=257+3; 262=257+5; | |
264=257+7; 266=263+3; 268=263+5; 270=263+7; 272=269+3; 274=271+3; 276=271+5; 278=271+7; | |
280=277+3; 282=277+5; 284=281+3; 286=283+3; 288=283+5; 290=283+7; 292=281+11; 294=283+11; | |
296=293+3; 298=293+5; 300=293+7; 302=283+19; 304=293+11; 306=293+13; 308=277+31; 310=307+3; | |
312=307+5; 314=311+3; 316=313+3; 318=313+5; 320=317+3; 322=317+5; 324=317+7; 326=313+13; | |
328=317+11; 330=317+13; 332=313+19; 334=331+3; 336=331+5; 338=331+7; 340=337+3; 342=337+5; | |
344=337+7; 346=317+29; 348=337+11; 350=347+3; 352=349+3; 354=349+5; 356=353+3; 358=353+5; | |
360=353+7; 362=359+3; 364=359+5; 366=359+7; 368=349+19; 370=367+3; 372=367+5; 374=367+7; | |
376=373+3; 378=373+5; 380=373+7; 382=379+3; 384=379+5; 386=383+3; 388=383+5; 390=383+7; | |
392=389+3; 394=389+5; 396=389+7; 398=379+19; 400=397+3; 402=397+5; 404=401+3; 406=401+5; | |
408=401+7; 410=397+13; 412=409+3; 414=409+5; 416=409+7; 418=401+17; 420=409+11; 422=419+3; | |
424=421+3; 426=421+5; 428=421+7; 430=419+11; 432=421+11; 434=431+3; 436=433+3; 438=433+5; | |
440=433+7; 442=439+3; 444=439+5; 446=443+3; 448=443+5; 450=443+7; 452=449+3; 454=449+5; | |
456=449+7; 458=439+19; 460=457+3; 462=457+5; 464=461+3; 466=463+3; 468=463+5; 470=467+3; | |
472=467+5; 474=467+7; 476=463+13; 478=467+11; 480=467+13; 482=479+3; 484=479+5; 486=479+7; | |
488=457+31; 490=487+3; 492=487+5; 494=491+3; 496=491+5; 498=491+7 | |
*/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment