Tuesday, July 27, 2010

Lothar

Rather than jump right into the Cocoa CoreAudio API, which no doubt will be causing me to pull out my few remaining hairs as I try to figure out what I could so easily do in Python on the PC or AppleScript on the Mac, I decided to first try a trial app as a sort of self-designed tutorial for how to write an iPhone app.

So I bought into the $99 developer's license and started to learn Objective-C. Objective-C isn't a particularly hard language, though the way everything seems to be a message and have only run-time binding is a bit tricky to get right.  But I'm learning.

The App I'm developing is called Lothar.  It is a simple number game that will compute all the numbers in the Collatz sequence — hence the name Lothar for Lothar Collatz and who recently celebrated (posthumously) his 100th birthday.  The algorithm is quite simple: if the number n is odd, then multiply n by 3 and add 1, making it even.  If the number is even, divide it by 2, which may still be even or may become odd.  When this algorithm has been applied to any positive integer it has always turned out that the sequence will end with the repeating subset 1, 4, 2.  By this I mean no-one has yet found a positive number for which this is not true, making this an undisputed theory, though not a fact.

Lothar also performs the reverse algorithm: when the number is odd, it is multiplied by 2, as well as when it is even and not equivalent to 1 modulo 3.  When the number is even and equivalent to 1 modulo 3 (e.g. 4, 10, 16, etc.), this number could have been arrived at in one of 2 ways using the Collatz algorithm: a) it was due to an odd number, so the previous number is (n - 1) ÷ 3 or b) it was due to an even number, so the previous number is n × 2.  This is handled by having a second button, labeled Multiply, appear.  Clicking Multiply performs b) and clicking Previous performs a).

Currently, the application is mostly complete.  There are only 2 concerns I have at the moment.  1) If you reverse the calculation enough, the number becomes so big it becomes negative.  Why this happens is a bit complicated to explain to someone who's never programmed before, but needless to say I need to adjust the application to prevent this from happening by disabling all buttons when the next result would overflow the number.  2) I want to create an Info page to tell the user a bit about me, TimeHorse, Multimedia and about the algorithm; I may even want to put a link to the Lothar Collatz wikipedia page.

Once that's done, I plan to submit it to the Apple Store.  After that, I have some ideas for enhancements, such as providing for negative number entry, allowing the user to enter custom algorithms, and so on.  But for now, I just want to get the application done and submitted and start the grueling work of my true calling: Tap to Go Back.

No comments:

Post a Comment