So, this version starts with the last version and applies a bit of smarts. Essentially, I said to myself, "Hey! The number of letters in the numbers 100 through 199 are the same as the number of letters in 1 through 99 plus 100 times the number of letters in "one hundred". I reworked the code to take advantage of that. Then I realized that the NumberLength function only needed to work on numbers between 1 and 99 inclusive, and reworked that as a special case. And as soon as I did that, it was obvious that NumberLength only had two cases, one for numbers between 1 and 20, one for 21 to 99, and just made two loops each of which had the appropriate piece of NumberLength code inside.

The resulting code runs in just a shade under two seconds on my MBP. That's a solid 90x improvement over my initial code. But Rakudo's overhead for starting the script is over one second. If you subtract that from all runs, this current version is under a second, and very close to be 200x faster than the original script.

## Wednesday, August 5, 2009

Subscribe to:
Post Comments (Atom)

## No comments:

## Post a Comment