Skip to content

Instantly share code, notes, and snippets.

@Iridium-IO
Last active October 31, 2018 13:45
Show Gist options
  • Save Iridium-IO/74aba5b0051ac07a529ca981451d93f1 to your computer and use it in GitHub Desktop.
Save Iridium-IO/74aba5b0051ac07a529ca981451d93f1 to your computer and use it in GitHub Desktop.
Counts the time needed to say all numbers up to 1 billion
Imports System.Threading
Module OneBillion
'Time needed to say numbers (in milliseconds)
Const unit = 158 'From 0-9
Const tens = 262 ' From 10-90
Const magnitude = 282 'time to say 'hundred, thousand, million'
Sub Main()
Dim summative As Long = 0
'Optimising this loop is left as an exercise to the reader
'(Hint: If it takes n seconds to say 'one hundred' and m seconds to say all numbers from 0-99,
'it takes 100n + m seconds to say all numbers from 100-199)
Parallel.For(0, 1000000000,
Sub(index As Integer)
Dim sumTime = CountTimeToSay(index.ToString("D10"))
Interlocked.Add(summative, sumTime)
End Sub)
Console.WriteLine(summative / 1000 & " seconds")
Console.Read()
End Sub
'I don't want to see this on r/badcode
Function CountTimeToSay(number As String) As Integer
Dim sumTime As Integer = 0
Dim Arr = number.ToArray
If Arr(9) <> "0" Then 'Units
sumTime += unit
End If
If Arr(8) <> "0" AndAlso Arr(8) <> "1" Then 'Tens, but treating the teens as units only
sumTime += tens
End If
If Arr(7) <> "0" Then 'Hundreds
sumTime += magnitude + unit
End If
If Arr(6) <> "0" Then 'Thousands
sumTime += magnitude + unit
ElseIf New String(Arr.Skip(4).Take(2).ToArray) <> "00" Then 'Checks to make sure a thousands value actually exists before adding the magnitude for it
sumTime += magnitude
End If
If Arr(5) <> "0" Then 'Ten Thousands
sumTime += tens
End If
If Arr(4) <> "0" Then 'Hundred Thousands
sumTime += magnitude + unit
End If
If Arr(3) <> "0" Then 'Millions
sumTime += magnitude + unit
ElseIf New String(Arr.Skip(1).Take(2).ToArray) <> "00" Then
sumTime += magnitude
End If
If Arr(2) <> "0" Then 'Ten Millions
sumTime += tens
End If
If Arr(1) <> "0" Then 'Hundred Millions
sumTime += magnitude + unit
End If
If Arr(0) <> "0" Then 'Billions
sumTime += magnitude + unit
End If
Return sumTime
End Function
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment