aboutsummaryrefslogtreecommitdiff
path: root/challenge-125/eric-cheung/excel-vba/ch-1.bas
blob: e31ee1dc563197c7bd00edb640a25402119a91f6 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
Attribute VB_Name = "ModTask_01"
Option Explicit
Public Const strMyTitle As String = "Eric Cheung"

Function IsPerfectSq(nNum As Integer) As Boolean
    If Sqr(nNum) - Int(Sqr(nNum)) = 0 Then
        IsPerfectSq = True
        Exit Function
    End If
    IsPerfectSq = False
End Function

Sub SortNum(ByRef nNum_01 As Integer, ByRef nNum_02 As Integer, Optional bSortAscOrder As Boolean = True)
    Dim nTempNum As Integer
    If _
        bSortAscOrder And nNum_01 > nNum_02 _
        Or Not bSortAscOrder And nNum_01 < nNum_02 _
    Then
        nTempNum = nNum_01
        nNum_01 = nNum_02
        nNum_02 = nTempNum
    End If
End Sub

Sub Task_01()
    ''https://www.cuemath.com/geometry/pythagorean-triples/
    
    Dim nInput As Integer, nTemp As Integer
    Dim nTriple_01 As Integer, nTriple_02 As Integer, nTriple_03 As Integer
    Dim nTriple_04 As Integer, nTriple_05 As Integer, nTriple_06 As Integer
    Dim nTriple_07 As Integer, nTriple_08 As Integer, nTriple_09 As Integer
    Dim strMsg As String
    Dim bFlag_01 As Boolean, bFlag_02 As Boolean
    
    nInput = 5
    
    '======================= SET 1, IF APPLICABLE =======================
    nTriple_01 = nInput
    
    If nInput Mod 2 = 0 Then
        nTriple_02 = nInput * nInput / 4 - 1
        nTriple_03 = nInput * nInput / 4 + 1
    Else
        nTriple_02 = nInput * nInput / 2 - 0.5
        nTriple_03 = nInput * nInput / 2 + 0.5
    End If
    
    If nTriple_02 = 0 Then
        MsgBox -1, vbOKOnly, strMyTitle
        Exit Sub
    End If

    SortNum nTriple_01, nTriple_02
    
    strMsg = "Set 1 Triple: " & nTriple_01 & ", " & nTriple_02 & ", " & nTriple_03
    '======================= SET 1, IF APPLICABLE =======================
    
    '======================= SET 2, IF APPLICABLE =======================
    bFlag_01 = False
    nTemp = (nInput + 1) * 4
    If IsPerfectSq(nTemp) Then
        nTriple_04 = Sqr(nTemp)
        nTriple_05 = nInput
        nTriple_06 = nInput + 2
        
        bFlag_01 = True
    End If
    
    If Not bFlag_01 Then
        nTemp = (nInput + 0.5) * 2
        If IsPerfectSq(nTemp) Then
            nTriple_04 = Sqr(nTemp)
            nTriple_05 = nInput
            nTriple_06 = nInput + 1
            
            bFlag_01 = True
        End If
    End If
    
    If bFlag_01 Then
        SortNum nTriple_04, nTriple_05
    End If
    
    If bFlag_01 Then
        'Same Set
        If _
            nTriple_01 = nTriple_04 _
            And nTriple_02 = nTriple_05 _
        Then
            bFlag_01 = False
        End If
    End If
    
    If bFlag_01 Then
        strMsg = strMsg & vbNewLine & "Set 2 Triple: " & nTriple_04 & ", " & nTriple_05 & ", " & nTriple_06
    End If
    '======================= SET 2, IF APPLICABLE =======================
    
    '======================= SET 3, IF APPLICABLE =======================
    bFlag_02 = False
    
    nTemp = (nInput - 1) * 4
    If IsPerfectSq(nTemp) Then
        nTriple_07 = Sqr(nTemp)
        nTriple_08 = nInput - 2
        nTriple_09 = nInput
        
        bFlag_02 = True
    End If
    
    If Not bFlag_02 Then
        nTemp = (nInput - 0.5) * 2
        If IsPerfectSq(nTemp) Then
            nTriple_07 = Sqr(nTemp)
            nTriple_08 = nInput - 1
            nTriple_09 = nInput
            
            bFlag_02 = True
        End If
    End If
    
    If bFlag_02 Then
        SortNum nTriple_07, nTriple_08
    End If
    
    If bFlag_02 Then
        strMsg = strMsg & vbNewLine
        If bFlag_01 Then
            strMsg = strMsg & "Set 3 Triple: "
        Else
            strMsg = strMsg & "Set 2 Triple: "
        End If
        strMsg = strMsg & nTriple_07 & ", " & nTriple_08 & ", " & nTriple_09
    End If
    '======================= SET 3, IF APPLICABLE =======================
    
    MsgBox strMsg, vbOKOnly, strMyTitle
    
End Sub