final int N = 11; String s = new String(new char[N]); while (s.length() > 1) { System.out.print(s.length() + ", "); s = s.replaceAll("(.+)\\1$|((.)+)", "$1$2$2$2$3"); } System.out.println(s.length()); // prints 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1
Thursday, February 3, 2011
Generating Collatz sequence using regex
See: Collatz conjecture on Wikipedia
Subscribe to:
Post Comments (Atom)
Nice one. As always, clever use of the tool.
ReplyDeleteYou should join RegExp Rehab. Yes, you do, first stage is denial. :P
ReplyDelete