Gaussian Summing of Digits!

Zness

New Member
Joined
Jan 5, 2014
Messages
14
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.

-------
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:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Just for fun :p

Code:
Sub MyResult()
Dim n As Long, lAdd As Long, i As Long, x As Integer, sGroup As String
Dim StartTime As Double, EndTime As Double, GaussNumber As Long

GaussNumber = Application.InputBox("Enter an integer. Press OK to sum every DIGIT between 0 and your Integer", _
                "Integer Entry", 0)
StartTime = Timer

For i = 1 To GaussNumber
    sGroup = CStr(i)
    For x = 1 To Len(sGroup)
        n = n + Mid(sGroup, x, 1)
    Next x
    lAdd = lAdd + n
    n = 0
Next i
        
EndTime = Timer

MsgBox "It took you " & Format(EndTime - StartTime, "0:00") & _
" seconds to ""MyResult"" witchyo digits!", vbOKOnly, "Answer = " & lAdd
        
End Sub
 
Upvote 0
Couldn't resist a formula-based offering:

Array formula**:

=SUM(IFERROR(0+MID(ROW(INDIRECT("1:"&A1)),TRANSPOSE(ROW(INDIRECT("1:"&LEN(A1)))),1),0))

though it no doubt takes a bit longer to calculate on a string like 1,000,000 than either of your scripts!

Edit: in fact, I'm sure the great Carl Friedrich could probably have calculated it faster in his head than my formula!

Regards


**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).
</SPAN></SPAN>
 
Last edited:
Upvote 0
I'm familiar with Array formulas, how do I use/set up the formula you made?

Well, that was simply set up to give the result for a value in A1, so you can amend if required, and also copy down to give results for other values.

Regards
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,926
Members
449,479
Latest member
nana abanyin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top