Random Firings from My Brain

March 24, 2008

The Nine Billion Names of God

Filed under: Uncategorized — Daniel @ 10:00 am

Arthur C. Clarke died last week (March 19). In memory of him, I read The Nine Billion Names of God which I had not read since college. Then, because I am somewhat OCD, I wrote a program to list the Nine Billion (or so) names of God given the following constraints: The English alphabet, no more than nine letters, and no letter will repeat more than 2 times. Its not a brilliant program(there are probably better ways to calculate the combinations of letters) but I wrote it long hand while my wife looked at me funny and then tested it the next day. Its written in java because for whatever reason, it came to pass in my career that I think best in java(in college I would have said it would be c but I don’t’ think I’ve written any c code for 7 years). Here it is(ugh, wordpress really screwed up the formatting of the code):

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("ninebillion.txt")));
char min = 'A';
char max = 'Z';
int numChars = 9;
int diff = (int)max - (int)min + 1;
for (int len = 1; len <= numChars; len ++) {
char count[] = new char[len];
for (int i = 0; i < count.length; i ++) {
count[i] = min;
}
int lim = (int)Math.pow((double)diff, (double)len);
for (int i = 0; i < lim; i++) {
count[0]++;
for (int j = 0; j < count.length; j++) {
if (count[j] > max) {
if (j < (count.length - 1)) {
count[j+1]++;
}
count[j] = min;
}
}
int numLetters = 0;
char lastLetter = 0;
boolean good = true;
for (int j = 0; (j < count.length) && (good); j++) {
if (count[j] == lastLetter) {
numLetters++;
if (numLetters > 2) good = false;
} else {
numLetters = 1;
}
lastLetter = count[j];
}
if (good) {
//String tmp = new String(count);
out.println(count);
}
}

}
out.close();
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: