All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Numbers
How many 1s? (Posted on 2004-07-26) Difficulty: 2 of 5
If I took all the numbers from one to one billion:
  1. How many of them contain at least one '1'?
  2. If I wrote them out in one long line, how many 1 digits would there be?

See The Solution Submitted by Thalamus    
Rating: 3.0000 (12 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
Solution Hey, you, get offa my cloud (Solution) | Comment 2 of 16 |

Sorry for the silly subject line -- it's Mick Jagger's birthday...

There are 612,579,512 numbers from 1 to a 1,000,000,000 with at least one 1, and there are 900,000,001 total 1's if they are all strung together.  

That is.... IF the following Visual Basic program, which ran in less than a second elapsed time, is correct....

Imports System
Imports System.IO
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Math
Module Module1
    Sub Main()
        Randomize()
        Dim strstarttime As String
        Dim strendtime As String
        Dim strend As String
        Dim dblstart As Double
        Dim dblnums As Double
        Dim dbltots As Double
        strstarttime = TimeOfDay
        Console.WriteLine("Start of execution: " & strstarttime)
        dblstart = 1
        dblnums = 1
        dbltots = 1
        While dblstart < 100000000
            dblstart *= 10
            dblnums += dblstart + (8 * dblnums)
            dbltots += dblstart + (9 * dbltots)
        End While
        dblnums += 1
        dbltots += 1
        Console.WriteLine("There are " & Str(dblnums) & _
        " numbers with at least one 1")
        Console.WriteLine("There are " & Str(dbltots) & _
        " total 1s")
        Console.WriteLine(" ")
        Console.WriteLine("Start of execution was: " & strstarttime)
        strendtime = TimeOfDay
        Console.WriteLine( _
        Str(1000000000))
        Console.WriteLine("End of execution: " & strendtime)
        strend = "?"
        Console.WriteLine(" ")
        While strend <> "X"
            Console.WriteLine("Please enter X to exit program...")
            strend = UCase(Console.ReadLine())
        End While
    End Sub
End Module 

Edited on July 26, 2004, 10:11 am
  Posted by Penny on 2004-07-26 10:10:26

Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (3)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information