I regularly participate on the site known as HackThisSite.org, it’s a great resource and community for those interested in hacking or in the general field of computers, internet and security. Recently in the user submitted challenges section I found a simple programming challenge that I decided to try and do.

The challenge was, in any language, find the first 25 “power numbers”. These are numbers that follow a rule of “if the sum of each digit in the number raised to the power of the length of the number is the number, it is a power number”.

My first swipe at this because I like a challenge was BATCH. However my BATCH skills have faded and I was spending way too much time at this, so I turned to Javascript which has been my soup de jour lately, and I’ve been advocating it as the future of everything so, why not some math?

Problem, Javascript runs on a single thread, and the calculation of 25 power numbers ends up with the following result: 0,1,2,3,4,5,6,7,8,9,153,370,371,407,1634,8208,9474,54748,92727,93084,548834,1741725,4210818,9800817,9926315

Yes, nearly 10 million iterations if it were done in a simple for loop. This could take on the order of hours and hours to calculate. So I did a little research and found, web workers. It’s a way to spin off processing within your browser as such that the main thread won’t get held up. It’s a pretty simple application but I’ve of course gone and made it into a dynamically generated crazy function. Here is a link to my power number finder in Javascript using X number of workers, based on your input.

I added some notes on that page as well. Namely, browsers are not created equal, Firefox seems to be king of the workers.. and as you add “threads” from 0 you will get better performance up until about 20, after which, it appears to go the other way, and starts missing some numbers… that could be because of my math function to find powered numbers though.

EDIT: I fixed my math function and wowsers, super performance increase on a massive order of magnititude, I’m definitely going to have to revisit this now.