Geek dad vs. Normal dad: A homework story

When I was in school, math homework was my nemesis. The seemingly endless (and at the time, seemingly pointless) repetition drove me crazy. As a kid, I wasn’t given much sense of the practical usefulness of any of the things I was being taught. I had much more important things to occupy my mind with, like Star Wars, Led Zeppelin, and astronomy.

I’m a dreamer. I am blessed with the type of personality that allows me to focus for long periods on things. If they interest me. Conversely, if something doesn’t interest me, to this day I still find it extremely hard to concentrate. I get especially irked if I sense that something is being done inefficiently or is unnecessarily repetitive. I mean, after all that’s why we invented computers, right?

Fast forward to 2009 and I’m helping my daughter with her math homework. She’s working on a sort of ‘math trick’ with number palindromes. That’s a number like 2447442 that reads the same forward and backward. One creates this via an iterative process. You reverse numbers and add them together. If you don’t have a palindrome you take the output of the previous operation and repeat until you get a palindrome. Or not.

Most of the first batch of problems went pretty well. My daughter was doing her work and I was checking the results with a calculator. Finally we came to the last couple of examples. Here’s where the problem started. It turned out we had misread one part of the instructions concerning how to handle decimals. Thus, we started out with a number that never became a palindrome. Sarah quickly gave up. Then my wife and I filled up a couple sheets of scratch paper with numbers that quickly spiraled into the Land of the Insanely Huge. I even went upstairs and banged out numbers on the calculator for twenty minutes. By this time, we were all miserable, so we agreed to take it up the next day.

So now I was irked (see above) and the wheels were turning. This wasn’t hard from a mathematical point of view. It just involved some juggling and a whole bunch of repetitive calculations. Perfect job for a script!

Any normal dad would’ve probably just re-read the instructions, fixed the calculation and called it good. Me? Geek dad? Nooooooo… I couldn’t leave it alone. I went upstairs and set to work on a command-line PHP script to expunge the drudgery from this thankless chore once and for all!

When it was done I had fun checking Sarah’s work, and discovered an earlier example she’d gotten wrong. For the record, we made sure our daughter understood the process and I only used the script to check her work, not give her the answers.

This is a total hack written late at night, so no flames please! Suggestions for improving it are welcome, however. I may create a web-ready version of this and stick it on the site if anybody’s interested in searching out new and amazing number palindromes. Aside from that, it’s now basically a solution to a nonexistent problem. And I can once again sleep at night.

#! /usr/bin/php

<?
	# The input number.

	$input = $argv[1];

	# Set up some variables
	$palindrome = 0;
	$limit = 10000; // how many iterations?
	$iterations = 0;

# ------------------------------------------------------------#
# Main 'while' loop
# ------------------------------------------------------------#

while($palindrome === 0 && $iterations <= $limit) {
	$iterations++;

	echo 'Testing: ' . $input . "\r\n";

	$result = testInput($input);

	if(is_numeric($result)) {
		echo "Palindrome found! : " . $result . "\r\n";
		break;
	} else {
		# next iteration, please.
		$input = genNewInput($input);
	}
} // end while

# ------------------------------------------------------------#
# Function: genNewInput
# ------------------------------------------------------------#
function genNewInput($input) {
	$fwd_array = str_split($input);
	$rev_array = array_reverse($fwd_array);

	# add reversed numbers together
	$fwd_num = implode($fwd_array);
	$rev_num = implode($rev_array);

	echo 'Adding: ' . $fwd_num  . ' + ' . $rev_num . "\r\n";

	$input = $fwd_num + $rev_num;
	return $input;
}

# ------------------------------------------------------------#
# Function: testInput
# ------------------------------------------------------------#
function testInput($input) {

	$fwd_array = str_split($input);
	$rev_array = array_reverse($fwd_array);

	# get the digit count
	$digit_count = count($fwd_array);

	$r = $digit_count - 1; // zero indexed

	# Determine if the output is a palindrome
	for($f=0;$f<$digit_count;$f++) {

		# inner loop. this takes the new input and tests it. If it passes, we've found our palindrome! If not, it continues the outer loop with the next input number.

		# compare the numbers.
		if($fwd_array[$f] === $fwd_array[$r]) {
			$r--;

			if($f === ($digit_count - 1)) {
				# BINGO!
				return $input;
			} else {
				continue;
			}

		} else {
			return false;
		}
	}
}

?>

[Note: I had originally posted more code here. I'm going to bring this back up as a separate page at a later date.]

I’d rather be lucky than good

Grass Carp

Copyright © 2009 Gawain Reifsnyder

Ever heard the old adage “I’d rather be lucky than good?” Sign me up. I must be luckier than that little green leprechaun on the front of my cereal box. While fishing for bass at an undisclosed location (Don’t ask. I won’t tell you.) I felt a massive jolt on the line. I didn’t know what I’d hit, but I could plainly see and feel that he was big. Real big. I had the sense to keep tension on the hook. I tried to stay calm and just let him take it out about six times until he tired out a bit. He’d kind of swim lazily around, pulling the line out into the middle of the lake and then thrash a couple of times, making a tremendous splash each time. Then he just sort of zoned out for a bit and let me reel him in part way. This dance went on for several minutes before I got him close enough to the bank to scoop him up in the net, which was almost too small to contain him.

Then I finally got a good look and realized what I had. He was of course a monster Ctenopharyngodon idella. This fish, otherwise known as a grass carp, was originally native to Asia and was introduced into the USA in 1963 for aquatic weed control. I knew there were some of these in the lake, but I was under the impression they weren’t interested in typical lures.

This one didn’t get the memo, apparently. As soon as I got him into the net I relaxed the tension on the line, and the little lure just popped out of his lip. He had just barely bitten down on it and if I had relaxed the tension for even a second while he was fighting I never would have gotten him to the bank.

Fortunately a young friend came by to help and was kind enough to take this picture of me with my prize. This fish was without question bigger than every other fish I’ve caught in my life. Combined. So as I let him slip back beneath the glassy surface I stood there with shaking hands and pounding heart and waved goodbye. It seemed only fitting to give him a name. So I did.

Holy Carp.

The 2009 Petit Le Mans at Road Atlanta

2009 American Le Mans Copyright © 2007 Gawain Reifsnyder

Copyright © 2007 Gawain Reifsnyder

Once again the calendar has rolled around to one of my favorite times of the year. The crisp fall air, the smell of race gas, the howl of a twin-turbocharged V-12 on the boil…

The Petit Le Mans is one of the most anticipated races on the American Le Mans Series calendar. It’s a 10-hour, 1,000 mile endurance race held at the 2.54-mile, 12-turn Road Atlanta circuit in Braselton, Ga. There are four different classes of cars racing on the same circuit at the same time. The speed differential between the different classes makes for some very interesting action and it’s one of the ultimate tests of man and machine in road racing.

With the exception of last year, the fall trip to Atlanta to watch the Petit Le Mans has become an annual pilgrimage of sorts for me. This year, the race is being held on September 26th, with events starting on the 23rd. Since I missed it last year, I may not be able to settle for anything less than a full dose, so I’ll probably head down to catch the practice sessions, qualifying and support races too.

If you’re interested in attending, you’ll find more information at the Road Atlanta or American Le Mans Series web sites.