We start the cycle again. The two lectures we have had basically comprised of syllabus day and an introduction to the first project: Collatz.

I have finished major portions of the Collatz project, but the one thing holding me up is getting pass the Sphere Online Judge. I am fairly confident that my code is producing the correct answers. However, the main point of contention is that The Judge seems to feel that my answer is to slow. At first, I tried the naive, no cache, solution. This was just to see what I was up against. Then I added an empty caching system with no offline calculations. Still not passing. I double-checked that my cache was working correctly and then proceeded to add some offline values into the cache. The Judge only allows a file size of 50KB, so there was a limit to the offline cache. I added values of the numbers with the largest cycle lengths. There were about five thousand or so added. This also did not work. Then I proceeded to add some other constraints. I computed all the Collatz sequences and checked for numbers that were ‘hit’ often and had high cycle counts. This also did not work. Well fine.

I changed up my strategy and started storing for ranges. I stored the max cycle length for ranges systematically from 1-999999 in the smallest increments possible. Larger increments resulted in smaller file size and vice-versa. Still timing out. Frustration.

An odd ball trick. I thought about encoding the cycle lengths into characters. Characters have 8 bytes. I need 10 bytes for each cycle length maximum. Using the encoding trick, I thought I could have smaller increments. I tried outputting that into a file and failed miserably because the text editor understood all the line-break, paragraph, and other formatting characters and would not put them into the string. If someone was watching me while I did this, I would be utterly embarrased.

Tip of the Week: Do not try encoding numbers into the bytes of a character if the range is too large.

Also, you can time snippets of code like this:

from timeit import Timer
Timer('str(100000)').timeit()