for (String s : new String(new char[(1 << 8) - 1]) .split("(?=(.*))(?<=(?<=(^.*))\\G.*)(?<=(?=\\2\\2.\\1)^.*)") ) { System.out.printf("%s ", s.length()); } // "1 2 4 8 16 32 64 128 "I think there's still room for "improvement", though...
"Improvements", as Java string literals:
"(?=(.*))(?<=(?<=(^.*))\\G.*)(?<=(?=\\2\\2.\\1)^.*)" "(?=(.*))(?<=(?<=(^.*))\\G.*)(?<=(?=\\2.\\1)\\G.*)"
Can you explain the regex a bit.
ReplyDeleteThe regex uses lookarounds, nested lookarounds, nesting a lookahead inside a lookbehind, capturing inside lookarounds, continuing previous match with \G, exploiting Java's regex engine bug by anchoring an infinite length lookbehind, etc.
ReplyDeleteProperties of sum of powers of two is also used for the arithmetic part.