View Single Post
  #23   Report Post  
Posted to microsoft.public.excel.programming
D-C Dave D-C Dave is offline
external usenet poster
 
Posts: 1
Default Can this Code be Condensed

I know this is a long thread and I'm late.
I'd like to enter this recursive approach.

Option Explicit

Dim ntype&(70)

Sub Main()
Dim zSum&, zStart#, i%
For i = 11 To 70
ntype(i) = 0
Next i
zStart = Timer
Application.ScreenUpdating = False
Range("B4").Select
Call Sub1(0, 1, 1) ' sum, first, level
For i = 11 To 70
ActiveCell.Offset(0, 0).Value = "Sum Of Digits ="
ActiveCell.Offset(0, 1).Value = i
ActiveCell.Offset(0, 2).Value = ntype(i)
ActiveCell.Offset(1, 0).Select
zSum = zSum + ntype(i)
Next i
ActiveCell.Offset(0, 0).Value = "Total Combinations Produced"
ActiveCell.Offset(0, 2).Value = zSum
ActiveCell.Offset(2, 0) = "This Program Took " & _
Format(((Timer - zStart) / 24 / 60 / 60), "hh:mm:ss") & " To Process "
Range("B68").Select
Application.ScreenUpdating = True
End Sub

Sub Sub1(pSum%, pFirst%, pLevel%)
Dim i1%
If pLevel < 7 Then
' recursively call sub1
For i1 = pFirst To 43 + pLevel
Call Sub1(pSum + i1 \ 10 + i1 Mod 10, i1 + 1, pLevel + 1)
Next i1
Exit Sub
End If
' level 7: add 1 to ntype
ntype(pSum) = ntype(pSum) + 1
End Sub

"Paul Black" wrote:
Is there any way that the code below can be condensed or shortened
please
It cycles through six number combinations and counts the total of ALL
the digits in each combination. So, combination 10,11,12,13,14,15 will
= 1+0+1+1+1+2+1+3+1+4+1+5 = 21.
The total of ALL the digits range from 11 to 70.
Her is the code :-

Option Explicit
Option Base 1

Sub Sum_Of_Digits()
Dim Start As Double
Start = Timer
Dim A As Integer
Dim B As Integer
Dim C As Integer
Dim D As Integer
Dim E As Integer
Dim F As Integer
Dim i As Integer
Dim nMinA As Integer
Dim nMaxF As Integer
Dim nType(70) As Double

Application.ScreenUpdating = False
Sheets("Results").Select
Range("B4").Select

nMinA = 1
nMaxF = 49

For i = 11 To 70
nType(i) = 0
Next i

For A = nMinA To nMaxF - 5
For B = A + 1 To nMaxF - 4
For C = B + 1 To nMaxF - 3
For D = C + 1 To nMaxF - 2
For E = D + 1 To nMaxF - 1
For F = E + 1 To nMaxF

If A \ 10 + A Mod 10 + B \ 10 + B Mod 10 + C \ 10 + C Mod 10 + D \ 10
+ D Mod 10 _
+ E \ 10 + E Mod 10 + F \ 10 + F Mod 10 = 1 Then nType(1) = nType(1)
+ 1
If A \ 10 + A Mod 10 + B \ 10 + B Mod 10 + C \ 10 + C Mod 10 + D \ 10
+ D Mod 10 _
+ E \ 10 + E Mod 10 + F \ 10 + F Mod 10 = 2 Then nType(2) = nType(2)
+ 1

[snip]
[SNIP SNIP]