In honor of Carl Friedrich Gauss I wrote a sub that sums every DIGIT (not every integer) between zero and the integer you choose. Let's take the integer 13 for example:
1+2+3+4+5+6+7+8+9+'1+0'+'1+1'+'1+2'+'1+3' = 55
or...
1+2+3...+999,999+1,000,000 = 27,000,001
Give it a try! Also, let me know what you think.... If you enjoy summing up digits Gaussian style.
-------
-----
Let me know what you think!
-Zness
1+2+3+4+5+6+7+8+9+'1+0'+'1+1'+'1+2'+'1+3' = 55
or...
1+2+3...+999,999+1,000,000 = 27,000,001
Give it a try! Also, let me know what you think.... If you enjoy summing up digits Gaussian style.
-------
Code:
Option Explicit
Sub Gauss()
Dim x As Double
Dim y As Integer
Dim Answer As Double
Dim StartTime As Double
Dim EndTime As Double
Dim GaussNumber As Long
GaussNumber = Application.InputBox("Enter an integer. Press OK to sum every DIGIT between 0 and your Integer", _
"Integer Entry", 0)
If GaussNumber = 0 Then Exit Sub
If GaussNumber = 1 Then
MsgBox "Answer = " & GaussNumber & ".... duh."
Exit Sub
End If
StartTime = Timer
Application.ScreenUpdating = False
Answer = 0
For x = 0 To GaussNumber
Select Case x
Case 0 To 8
For y = 1 To Len(CStr(x))
Answer = Answer + CInt(Mid(CStr(x), y, 1))
Next y
Case 9
Answer = Answer + 9
GoTo AdvanceX
Case Else
For y = 1 To Len(CStr(x))
Answer = Answer + CInt(Mid(CStr(x), y, 1))
Next y
End Select
AdvanceX:
Next x
Application.ScreenUpdating = True
EndTime = Timer
MsgBox "It took you " & Round(EndTime - StartTime, 2) & _
" seconds to ""GET GAUSSIAN"" witchyo digits!", vbOKOnly, "Answer = " & Answer
End Sub
Let me know what you think!
-Zness
Last edited by a moderator: