If I took all the numbers from one to one billion:
- How many of them contain at least one '1'?
- If I wrote them out in one long line, how many 1 digits would there be?
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 |