I spent a little while trying to come up with some inductive (or other clever) way to compute the sum of the digits of , without just implementing multiplication by 2. Mostly I did this because I figure is pretty big, and maybe having a script compute that number would cause some sort of issue. Not coming up with anything particularly clever, I thought I’d try just a naive approach: compute directly and then sum the digits. Here’s the one-liner (not counting “import sys”) I came up with:

print sum(map(int,str(int(sys.argv[1]))))

Delightful. Even better – my initial concern that something would go wrong (take a while, use up too much memory) was apparently unfounded. This script ran without any noticeable difficulty in just a fraction of a second. It also did fine with a power of 10,000 and 100,000 (taking a little more than a second here). For 1,000,000, it took close to 2 minutes. This still makes me wonder if there’s a clever way to solve this problem quickly for big powers.

I thought I’d see what the function the sum of the digits of looked like. Here’s a sampler:

Kinda fun, I think. Then I thought: how about just sum of the digits of . I encourage you to spend up to 10 seconds thinking what this graph should look like.

### Like this:

Like Loading...

*Related*

Tags: nah, python

This entry was posted on July 1, 2009 at 11:12 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

July 17, 2009 at 3:45 pm |

[…] problem is pretty similar to Problem 16, which I also wrote about. There are probably improvements to be made. Like, you might as well start the xrange at 2, instead […]