A Collatz sequence is defined for the set of positive integers:
n → n/2 (n is even) n → 3n + 1 (n is odd)
Using this rule, a sequence can be generated.
Which starting number, under one million, produces the longest chain?
OBTW WHICH STARTIN NUMBR, UNDR WAN MILLION, PRODUCEZ TEH LONGEST CHAIN? TLDR HAI 1.3 HOW IZ I collatz YR currentNumber I HAS A cowntr ITZ 1 IM IN YR collatzLoop BOTH SAEM 0 AN MOD OF currentNumber AN 2, O RLY? YA RLY, currentNumber R QUOSHUNT OF currentNumber AN 2 NO WAI, currentNumber R SUM OF PRODUKT OF currentNumber AN 3 AN 1 OIC cowntr R SUM OF cowntr AN 1 BOTH SAEM currentNumber AN 1, O RLY? YA RLY, FOUND YR cowntr, OIC IM OUTTA YR collatzLoop IF U SAY SO HOW IZ I longestCollatz YR maxNumber VISIBLE maxNumber I HAS A maxLengthFound ITZ 0 I HAS A currentLength ITZ 0 I HAS A longestAtValue ITZ 0 I HAS A cowntr ITZ 1 IM IN YR maxCollatzLoop currentLength R I IZ collatz YR cowntr MKAY DIFFRINT currentLength AN SMALLR OF currentLength AN maxLengthFound, O RLY? YA RLY, maxLengthFound R currentLength, longestAtValue R cowntr, OIC cowntr R SUM OF cowntr AN 1 BOTH SAEM cowntr AN maxNumber, O RLY? YA RLY, FOUND YR longestAtValue, OIC IM OUTTA YR maxCollatzLoop IF U SAY SO VISIBLE I IZ longestCollatz YR 1000000 MKAY KTHXBYE
Took only 3 minutes to execute on my machine, lightning fast!
Of-course, in any real language with a real implementation, this should take under 5 seconds. But lolcode isn't very fast and it's lacking a lot of performance constructs. For example, usually you'd memoization or recursion. I'm pretty sure the latter can't be done and the former would get very ugly very fast.
How can I take advantage of the features that are available in this language to the fullest extent to improve the performance of my program?