aboutsummaryrefslogtreecommitdiff
path: root/challenge-136/eric-cheung/excel-vba/ch-2.bas
blob: c1c840ee69f4eb52f365c05f2ca810d1b0ca1a39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
Attribute VB_Name = "ModTask_02"
Option Explicit

Public arrFibonSeq() As Variant

Sub GenFibonSeq(ByVal nNum As Integer)

    Dim nLoop As Integer, nSum As Integer
    
    nLoop = 2
    ReDim arrFibonSeq(1 To nLoop)
    
    arrFibonSeq(1) = 1
    arrFibonSeq(2) = 2
    
    Do
        nSum = arrFibonSeq(nLoop) + arrFibonSeq(nLoop - 1)
        If nSum > nNum Then Exit Sub
        nLoop = nLoop + 1
        ReDim Preserve arrFibonSeq(1 To nLoop)
        arrFibonSeq(nLoop) = nSum
    Loop Until nSum > nNum

End Sub

Sub Task_02()

    '' Const nInput As Integer = 16 '' Example 1
    '' Const nInput As Integer = 9 '' Example 2
    Const nInput As Integer = 15 '' Example 3

    Dim nArrSize As Integer, nWayCount As Integer, nIndxLoop As Integer, nLastIndx As Integer, nLoopSum As Integer
    Dim bFlag As Boolean
    
    If _
        nInput = 1 _
        Or nInput = 2 _
    Then
        nWayCount = 1
    Else
        nWayCount = 0
    
        GenFibonSeq (nInput)
        nArrSize = UBound(arrFibonSeq) - LBound(arrFibonSeq) + 1
        
        nLastIndx = nArrSize
        
        Do While nLastIndx > 1
            nLoopSum = nInput
            For nIndxLoop = nLastIndx To 1 Step -1
                If nLoopSum >= arrFibonSeq(nIndxLoop) Then
                    bFlag = False
                    
                    If _
                        nLoopSum = arrFibonSeq(nIndxLoop) _
                        And nIndxLoop < nArrSize _
                        And nIndxLoop >= 3 _
                    Then
                        bFlag = True
                    End If
                    
                    nLoopSum = nLoopSum - arrFibonSeq(nIndxLoop)
                    
                    If nLoopSum = 0 Then
                        nWayCount = nWayCount + 1
                        If bFlag Then
                            nWayCount = nWayCount + 1
                        End If
                        Exit For
                    End If

                End If
            Next nIndxLoop
            
            nLastIndx = nLastIndx - 1
        Loop
    End If

    MsgBox nWayCount, vbOKOnly, strMyTitle

End Sub