1. Computing & Technology

Discuss in my forum

Programming Challenge 25 - Count the Ones

By , About.com Guide

This challenge has been stopped early. See I got it wrong.

There is a new challenge which runs to the end of July.

The Old Discontinued Challenge
Really simple challenge this time, how fast can your program count from 1 to 99,999,999 (1 short of 100 million) and total up how many 1s were there.

Eg 1 = 1 111 has 3 1s. so does 1101, 11351 etc.

That's all there is to it.

Your Entry

Must count in some way the number of 1 digits in the numbers between 1 and 99,999,999. Feel free to optimize it in any way except pre-calculation. Also please time how long it takes. If it's well under a second, please redo in a loop so it takes about a second and output the average time.

The output should be something like

 Time to solve (once): 0.0476 seconds. Total = 
 
This is the code that you can use for timing.

Rules

This is for glory only. About.com does not permit prizes to be given.

Please submit your source code and the output file to the cplus.guide@about.com?subject=Programming Challenge 25 email address with the subject line Programming Challenge 25.

It must compile with Open Watcom, Microsoft Visual C++ 2005/2008 Express Edition/Microsoft Visual Studio 2003/2005/2008 or Borland Turbo C++ Explorer, Microsoft Visual C# 2005/2008 Express Edition or GCC/G++. If it doesn't compile, it can't be run so is automatically disqualified

Please include your name, age (optional), blog/website url (optional) and country. Your email address will not be kept, used or displayed except to acknowledge your challenge entry. You can submit as many entries as you like before the deadline which is June 30 2009.

The top ten entries will be listed, judged purely on speed. A condition of entry is that you allow your source code to be published on this website, with full credits to you as the author.

Have fun!

More Programming Challenges

©2012 About.com. All rights reserved.

A part of The New York Times Company.