Wednesday, February 29, 2012

Book Review: Me Talk Pretty One Day

Want to read hilarious non-fiction: get a copy of "Me Talk Pretty One Day" by David Sedaris.

David Sedaris writes about his observations of the world, growing up, getting a job, living in Chicago. While most people shrug nasty encounters or get mad about them, David finds a funny twist and brings out the humor in them.

This book is a disconnected collection of his experiences growing up, moving out of his parental home, and learning French. Some essays are brilliantly insightful, where he discusses how the French language has gender for inanimate objects and how he constructed stories involving household objects like Mr. Belt to drive this point home. Or his description of religious festivities and symbology in the chapter hilariously titled, "Jesus Shaves". David also talks about his family and their antics. The best chapter was the one in which he describes his working life as a mover: how he accompanied a schizophrenic, a murderer and a communist in the moving business. His murderer friend says to him, "I can't say I'll never murder another person. It's unrealistic to live your life within such rigid parameters."

Sometimes you wonder, is David telling the truth? Is it true that his father stores away food for later consumption. Then you realize that every family has quirks, and indeed everyone is surrounded by quirky people. It takes David's eye to find funniness in everything and to show it to others.

(Image courtesy Wikipedia)

Tuesday, February 28, 2012

Book Review: India - From Midnight to Millenium

Want to read some non-fiction about Indian politics and culture, read "India: From Midnight to Millenium", by Shashi Tharoor.

Much like other non-fiction by Shashi Tharoor, this book talks about India in some depth. It is a collection of essays about Indian political and cultural life. There were many interesting facts here. I loved his chapter on the Indian diaspora, including the hilarious tidbit that there was indeed an Indian merchant who maintained a supplies shop near the North Pole. Sounds like the setting for a perfect joke, doesn't it? The book talks about Shashi's experience as a Keralite who grew up in Bombay and then the US. I can feel his sense of detachment from his home-state as a youth, having grown up away from it. He also talks about how he defended the Emergency as an Indian abroad. At the time, American press was critical of Indira Gandhi. The book has some valuable points, about how India has developed politically in the presence of divisive elements like religion, language and ethnic affiliation.

The chapter on Indian economic liberalization was beautiful. It talked of how the Indian economy was forced to open due to a balance of payments crisis. Shashi talks of how politically sensitive liberalization was in the past. After the liberalization has improved the lives of middle-class Indians, it is unstoppable. No politician from either side wishes to turn back the clock and bring India back to the socialist license regime. Another chapter on the Indian political class was insightful. Shashi mentioned how Indira Gandhi had risen to the post of Indian Prime Minister, and how her son was hurriedly drafted for the job after she was assassinated. Other observations were equally fascinating: how Rajiv Gandhi's assassination helped the Congress by rallying the nation behind them for the sympathy vote.

Unfortunately, my book was hard to read. Physically hard to read. I have a paperback, and the binding of the book was uncomfortably rigid. For much of the book, I had to hold the stiff binding apart with both hands. I am too careful with books to crack the spine, something I should have done early with this book. Penguin publishers should look into better binding, one that doesn't cause aching hands after reading a book.

One shortcoming of the book is that it often diverges from the main point. For example, while talking about India, Shashi diverges to talk about Kerala, beginning the diversion with something along the lines of, "I hail from Kerala, a southern state in India, whose language Malayalam is the only language whose name is a palindrome in English." This fact of Malayalam being a palindrome is neither central to the discussion, nor a very interesting one. In addition to frequent diversions, the book labors some points repeatedly, like Kerala's high literacy rate. However, this is to be expected from any collection of essays by a single author.

The only strange chapter was the one discussing the incident of the elephant god Ganesh drinking milk in 1995 where Shashi claims that he was witness to this miracle. It is sad to see an educated person admit to such mumbo-jumbo. At the end of the chapter, there was mention of capillary action, even though capillary action cannot explain how metallic statues were reported to drink vast quantities of milk. Either Shashi is indeed religious or has acknowledged defeat in face of strong Indian sentiment.

So read this book as a collection of observations about Indian culture and political development. This book isn't as insightful as V.S. Naipaul's observations on India, but it does show how India changed after independence.

Monday, February 27, 2012

Project Euler in R: Problem 26

I have been posting R solutions to Project Euler problems as a way of polishing my R skills. Here is the next problem in the series, problem 26. The problem is stated as follows:

A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:
1/2 = 0.5
1/3 = 0.(3)
1/4 = 0.25
1/5 = 0.2
1/6 = 0.1(6)
1/7 = 0.(142857)
1/8 = 0.125
1/9 = 0.(1)
1/10 = 0.1
Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle. Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
The first task is to define a function that returns the cycle length of 1/n for any input n. The function logic is pretty simple.  We compute all the remainders in the long division of 1 by n. The moment we encounter a remainder that has been seen before, we know that the fraction has a recurring cycle and can compute its length.

recur.length <- function (n) {
  x = 1                             # Starting with the numerator 1
  remainders = c(x)
  repeat {
    x = (x*10) %% n                   # Find the remainder
    if (x == 0) { return (0) }        # Fraction terminates, so cycle length is 0.
    if ((x %in% remainders)) {break}  # Repeating remainder is found. Break from loop.
    remainders <- c(remainders, x)    # Else add remainder to list.
  return (length(remainders) - which(remainders == x) + 1)  # Exclude non-repeating part of the decimal
The above code demonstrates the use of the less common but simple repeat loop.  R doesn't have a do-while, so repeat with a break is a perfect way to imitate such a construct.  You can read more about R control-flows here.

Using the recur.length function we find d < 1000 with the largest recurring cycle. The following insights that make our search pretty efficient

  • A number d can have a recurring cycle of at most d - 1.  This is because modulo division by d can have only d unique remainders (0, 1, ..., d-1), after which we will necessarily find a remainder that repeats. One of these remainders is zero, and if we get this, no recurrence is possible. 
  • Due to the first observation, larger values of d will possibly have larger cycle lengths.  So it is faster to searching from 1000 down to 2, rather than from 2 up to 1000.
  • Searching down from 1000, say we find a recurring cycle of length k.  Now we know that the smallest number that we need to check is k + 1.  This is because the numbers smaller than k + 1 (that is 2 to k) can only have cycle lengths of k - 1 or less.

Based on the above, here's a function to find the d with the largest recurring cycle. 
longest <- function (n) {
  maxlength = 0       # Maximum recurring cycle
  maxd = 0            # d with the maximum recurring cycle
  # Check all numbers between low and i for a longer cycle length
  low = 2
  i = n
  while (i > low) {
    ilength = recur.length(i)
    if (ilength > maxlength) {
      maxlength = ilength
      maxd = i
      low = maxlength + 1           # The candidate must be > = low
    i = i - 1
The search is efficient and takes only 0.15s on my Intel Core 2 Duo 1.6Ghz laptop running Linux.  Discussion threads suggest limiting the search to prime numbers, but I'm not sure if this would be much more efficient.  The gains in the reduced number of candidates would probably be offset by primality tests. It is amazing how small this code is. R handles most of the array handling and searching through a vector.

Code formatted by Pretty R at

Saturday, February 18, 2012

App store restrictions hurt creativity

Creativity is risky

There is a lot of talk of creativity and its formal sibling: innovation. Companies love to talk about how innovative their new gadget is, even if it is a marginal improvement over the previous product. The word innovation gets thrown around a lot, even though true innovation is rare. It seems like every other story about American competitiveness is about spurring innovation and boosting creativity.

Let's listen to one of the most creative game developers of all time, Tim Schafer. This guy is so good that even Yahtzee, the Zero Punctuation game reviewer, found Tim's game Psychonauts to be fantastic. It is one of the rare times that Yahtzee has loved a game, so it pays to take notice. Game players love Tim Schafer's games too. For his next game, Tim Schafer asked donors to pledge money towards the development of the next game. He was hoping for $400k in a month. He got it in less than eight hours. So creativity is still rewarded, except that mainstream game distributors won't touch anything creative: it is too risky. By definition creative and innovative stuff is risky. If it wasn't risky, it would be a minor improvement over an existing formula. Most creative stuff lies somewhere between the vast lands of very risky and the seas of batshit crazy.  Minecraft was in the batshit crazy realm for me. To this day I cannot understand what the lure is, despite having bought it and played it. But clearly it is popular, and its creator, Notch deserves every penny of his wealth for having tried something risky.

All this suggests that creativity is well and alive, and the US has even found way of rewarding it. Tim and Notch seem to have gotten rich by merit alone.

Yet the world of an indie developer is precarious. It is difficult to get noticed, and it is difficult to maintain a connection with the consumer when you are a small guy. Everyone has heard of Nintendo, and when they come out with another crappy Zelda remake, at least I can explain the background to my mom. She might not understand gaming, but even she has heard of the Wii. So any game by the Wii guys might be a good gift for the grandkids. Independent game developers aren't stocked at Best Buy or Walmart. They rely on selling direct to customer: it is a high risk, low margin proposition. The biggest problem is gaining exposure and getting customers to pay up and try the game.

The most innovative idea in Indie game distribution has been the indie bundle. The first one appeared four years ago, and it is such a successful idea that this year there was a small army of competing bundles. This year Steam had four or five bundles, and it was hard to remember what the original was. The Indie guys even did an Indie bundle for Android recently. They sold four high-quality Android games, and collected a million dollars from people like me. The games are refreshingly innovative: a cube moving in a maze, another about an ameoba eating everyone smaller than it.

No Indie for iStore

In all this love-fest, it is easy to forget  that there was no Indie bundle for iPhone.


Oh yeah, that other phone platform. There can be no Indie bundle for the iPhone, given the extremely restrictive app store model. On an iPhone, users cannot install software from any source. They can only install software from the App store. And once they buy an app, they cannot download the installer for future use. They must download it every time from the App store. The cost is fixed: it is a single price per country. And Apple gets a 30% cut from it. Each of these restrictions makes the indie bundle model difficult.

The Indie bundle relies on consumers naming their price. If they pay more than the average amount, they get some extra goodies. Other than this, the games you download for $10 are the very same you download for $20. All the games are unlocked, with no copy protection. The indie model works well: you pay once and you have the game forever. You can keep a copy of the game installer (.apk file) around and install it on any of the multiple devices that you may own.

I even let my wife try out one of the games by installing it on her Android phone. She didn't pay for it! Oh, no. But this is fine, even encouraged under the terms of the Indie Bundle.  This is how the creative and innovative ideas take hold.

Desktop systems will restrict applications

Due to the restrictions in its App Store, an iPhone Indie bundle will be nearly impossible. But at least you can get these games on the Mac.  Not for long. The desktop world is moving towards restricting applications as well.

Exhibit #1 is Microsoft's next operating system. Metro applications are the new style of Windows applications. These can only be downloaded and installed from Microsoft's app store. No indie nothing, all developers have to go through the App store. An App store in itself is not bad, but at the current low margins, the developers would love to sell direct to the customer. Osmos, one of the games in the Indie bundle, is sold directly on their site for Android, Mac and Linux.  (Of course they cannot sell it directly for iPhone, since iPhone users cannot install anything outside the App store).  Tim Schafer's next game will be sold direct to customers as well. It's the only way small developers can make money with their risky innovation and their slim margins.

Exhibit #2 is Apple. In Mountain Lion, Apple hopes to include a feature which will only allow applications from their own app store. For now, there are three levels of security: (A) you can install App-store only, (B) App-store plus some good guys, and (C) anyone. You can select any one of these options today. My guess is that a future update will require a system to be restricted to choice A: app-store purchased apps only. This is the most restrictive, and it allows Apple the greatest control over your computer. Perhaps Apple will come out with a new version of GarageBand, which is free if your OS is restricted to App-store only apps. These choices are managed by the software called Gatekeeper. The write-up on Gatekeeper is an exercise in double-speak. From their website,

What is malware?

Malware is malicious software that can come from anywhere on the web. Anyone can unknowingly download and install malware because it’s deviously disguised as something else. Once you’re tricked into downloading it, malware can damage your computer and even gather sensitive information about you. While malware is one of the biggest security challenges on personal computers, it’s hardly an issue on a Mac. And Apple is working hard to see that it won’t be.

They admit that malware isn't a problem on a Mac, though Mac has been unrestricted till now. So there really is no reason for restricting installs to an app store from the malware perspective. This talk of restricting installs is nonsense for very good reasons.

First, even Apple's curated and anointed iPhone apps leak more private data than the apps on an open store on the same platform [PDF of research study]. So much so for the high trust blessed apps.

Second, Apple's desktop software has been wide open for years, and it hasn't had any problem with malware. Malware is a direct result of poor system design. Microsoft Windows had malware because every program was running as administrator. Every system has bugs, and when every software runs as administrator, every bug can lead to the attacker taking full control over the system. Mac OS had a Unix-style separation of administrator and user. Bugs in the browser don't lead to the entire system being compromised. This is why Mac OS doesn't have malware, not because some cool dudes in turtlenecks are ensuring the high pedigree of every app.

The only winner in the restrictive app store model is the app store owner. At the end of the day, 30% of all revenue is a pretty good deal for moving bits around, especially when the Internet is already doing all the heavy lifting.

(Image courtesy Hugh MacLeod)

Tuesday, February 14, 2012

Book Review: An Area Of Darkness

Want to learn about the real India? Read "An Area Of Darkness", by V.S. Naipaul.

I have just started reading An Area Of Darkness, and I already love the book. There are few books that describe India as it is, and this is one of them. I had read V.S. Naipaul's, "A Wounded Civilization" earlier, and had loved it. This is another book that shows remarkable insight into India.

I grew up in India, and struggled with India much of my life. I have often wondered about India's vast problems (poverty, illiteracy, corruption) and tried to think of the reasons for these problems. I'm not a scholar or a philosopher, and my wonder and search has been fruitless. Many years ago, I read V. S. Naipaul's, "A Wounded Civilization" and was struck by the depth of understanding. Not only did see many of the same problems I encountered but he diagnosed the root cause. When I read that book, I was struck by how accurate he was about the problems. And I was a little disappointed. Many problems he mentioned were still around, decades later. I wasn't very hopeful about the future.

"An Area Of Darkness" is similar. It is pessimistic, in the same way that the diagnosis of high cholesterol is pessimistic. V. S. Naipaul writes about the world that he sees. He is originally from Trinidad, so he notices a lot of strange behavior in India. He is a curious outsider: ethnically an Indian, but removed from Indian influence long enough that the quirks stand out. The book is bitter-sweet. It is heartening that someone has been able to locate the root cause for some of our misery. Yet, it is appalling that we haven't done much about it.

The book was published in 1964.

While I can't say that Indian society has stayed still for over four decades, there is too little progress. Too little to matter anyway.

It is difficult to recommend this book. I am sure many people will taken offense at the honesty. I am sure many people see things differently. In my experience, nothing talks about India in a more clear and insightful way than this book.

Only a correct diagnosis can produce a successful remedy.

Image courtesy Wikipedia.