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:
There is a much faster way of calculating the Gaussian Sum Of Digits:

For the last digit of the integer, you sum 1 through digit (1+2+3 etc)
For the other digits of the integer, you sum 3 parts:
a. Digit x SUM(1:9) x 10^(digit number - 1) (where last digit = digit number 1; second last digit = digit number 2, etc).
b. Digit x (previous digits + 1).
c. SUM(1 through (digit-1)) x 10^(digit number - 1) (0 if digit <= 1).

The final outcome is the sum of outcomes for each digit.

----------------

Example integer: 903,047

Last digit (7): 1+2+3+4+5+6+7 = 28

Second last digit (4):
a. 4x(SUM(1:9)x10^1) = 4x45 = 180.
b. 4x(7+1) = 32.
c. SUM(1 through 4-1))x10^1 = (1+2+3)x10= 60.
Total: 180+32+60=272.

Third last digit (0):
a. 0x(SUM(1:9)x10^2) = 0x900 = 0.
b. 0x(47+1) = 0.
c. 0 (because digit<=1).

Fourth last digit (3):
a. 3x(SUM(1:9)x10^3) = 3x13,500 = 40,500.
b. 3x(047+1) = 144.
c. SUM(1 through 3-1)) x 10^3 = (1+2)x1,000 = 3,000.
Total: 40,500+144+3,000 = 43,644.

Fifth last digit (0) delivers 0 (just like third last digit)

Sixth last digit (9):
a. 9x(SUM(1:9)x10^5)= 9x2,250,000 = 20,250,000.
b. 9x(03047+1) = 9x3,048 = 27,432.
c. SUM(1 through 9-1)) X 10^5 = (1+2+3+4+5+6+7+8)x1,000 = 36x1,000 = 36,000.
Total: 20,250,000+27,432+36,000 = 23,877,432.

Gaussian Sum Of Digits for 903,047: 28 + 272 + 0 + 43,644 + 0 + 23,877,432 = 23,921,376.
 
Last edited:
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Perhaps
Code:
Sub test()
    Dim GaussNumber As Long
    
    GaussNumber = Application.InputBox("Enter an integer. Press OK to sum every DIGIT between 0 and your Integer", "Integer Entry", Default:="13", Type:=1)
    If GaussNumber < 1 Then Exit Sub: Rem canceled
    
    MsgBox GaussSumTo(GaussNumber)
End Sub

Function GaussSumTo(N As Long)
    Dim i As Long
    For i = 1 To N
        GaussSumTo = GaussSumTo + GNumeralSum(i)
    Next i
End Function

Function GNumeralSum(aNumber As Variant) As Long
    If Len(CStr(aNumber)) < 2 Then
        GNumeralSum = Val(aNumber)
    Else
        GNumeralSum = Val(Left(CStr(aNumber), 1)) + GNumeralSum(Mid(CStr(aNumber), 2))
    End If
End Function
 
Last edited:
Upvote 0
Code:
Sub Gauss()
Dim X As Long, Y As Long, Z As Long
For X = 1 To InputBox("Enter an integer and press OK", "Gaussian input Entry", 0)
    For Y = 1 To Len(CStr(X))
        Z = Z + CLng(Mid(CStr(X), Y, 1))
    Next
Next
MsgBox "Answer = " & Z
End Sub
 
Upvote 0
Was hoping to do it without the inner loop but I couldn't work out a way to use split() on 1 char rather than using an actual char to split on.
 
Upvote 0
Let me know what you think!

Well, with all due respect I think it's not really Gaussian style to loop from 1 to the provided integer.


I adjusted your code to loop through just the number of digits in the provided integer. For explanation, see my post (5 posts up).

Code:
Option Explicit
Sub GaussMB()

    Dim x As Double
    Dim y As Integer
    Dim Digit As Integer
    Dim L As Integer
    Dim PreviousPlus1 As Long
    Dim PartA As Long
    Dim PartB As Long
    Dim PartC As Long
    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
    L = Len(CStr(GaussNumber))
    Digit = CInt(Mid(CStr(GaussNumber), L, 1))
    PreviousPlus1 = Digit + 1
    Answer = 0
    For x = 1 To Digit
        Answer = Answer + x
    Next x
    For x = 1 To L - 1
        Digit = CInt(Mid(CStr(GaussNumber), L - x, 1))
        If Digit <> 0 Then
            PartA = Digit * 45 * x * 10 ^ (x - 1)
            PartB = Digit * PreviousPlus1
            PartC = 0
            If Digit > 1 Then
                For y = 1 To Digit - 1
                    PartC = PartC + y * 10 ^ x
                Next y
            End If
            Answer = Answer + PartA + PartB + PartC
            PreviousPlus1 = PreviousPlus1 + Digit * 10 ^ x
        End If
    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
 
Upvote 0
Well, with all due respect I think it's not really Gaussian style to loop from 1 to the provided integer.
I adjusted your code to loop through just the number of digits in the provided integer. For explanation, see my post (5 posts up).

This thread is getting awesome! In response to your quote, the inspiration for me to attempt this code was this video....
https://www.youtube.com/watch?v=Dd81F6-Ar_0
 
Upvote 0

Forum statistics

Threads
1,215,593
Messages
6,125,715
Members
449,254
Latest member
Eva146

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