aboutsummaryrefslogtreecommitdiff
path: root/challenge-141/eric-cheung/excel-vba/ch-1.bas
blob: a7438161801a32bd4a37a96b176e879fd309dd3d (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
Attribute VB_Name = "ModTask_01"
Option Explicit

Public Const strMyTitle As String = "Eric Cheung"

Option Base 1
Public nPrimeArr() As Long
Public nPrimePow() As Long

Function IsPrime(nInput As Long) As Boolean

    Dim nNumLoop As Integer, nTempInt As Long
    Dim bFlag As Boolean
    
    bFlag = False

    For nNumLoop = LBound(nPrimeArr) To UBound(nPrimeArr)
        nPrimePow(nNumLoop) = 0
    Next nNumLoop
    
    nNumLoop = LBound(nPrimeArr)
    nTempInt = nInput
    
    Do While nNumLoop <= UBound(nPrimeArr)
        If nTempInt Mod nPrimeArr(nNumLoop) = 0 Then
            nTempInt = nTempInt / nPrimeArr(nNumLoop)
            nPrimePow(nNumLoop) = nPrimePow(nNumLoop) + 1
            bFlag = True
        Else
            nNumLoop = nNumLoop + 1
        End If
    Loop

    If bFlag Then
        IsPrime = False
    Else
        IsPrime = True
    End If
    
End Function

Function CountNumFactor() As Integer

    Dim nNumLoop As Integer
    CountNumFactor = 1
    
    For nNumLoop = LBound(nPrimePow) To UBound(nPrimePow)
        CountNumFactor = CountNumFactor * (nPrimePow(nNumLoop) + 1)
    Next nNumLoop

End Function

Sub Task_01()

    Const nNumDiv As Integer = 8

    Dim strMsg As String
    Dim nLoop As Long, nCnt As Integer, nPrimeCnt As Long
    
    ReDim nPrimeArr(1 To 1)
    ReDim nPrimePow(1 To 1)
    
    nPrimeCnt = 1
    nPrimeArr(1) = 2
    
    nLoop = 3
    nCnt = 0

    Do While (nCnt < 10)
        If Not IsPrime(nLoop) Then
            If CountNumFactor() = nNumDiv Then
                If strMsg <> "" Then
                    strMsg = strMsg & ", "
                End If
                strMsg = strMsg & nLoop
                nCnt = nCnt + 1
            End If
        Else
            nPrimeCnt = nPrimeCnt + 1
            ReDim Preserve nPrimeArr(1 To nPrimeCnt)
            ReDim nPrimePow(1 To nPrimeCnt)
            nPrimeArr(nPrimeCnt) = nLoop
        End If
        nLoop = nLoop + 1
    Loop
    
    strMsg = "First 10 positive integers having exactly " & nNumDiv & " divisors are: " & strMsg

    MsgBox strMsg, vbOKOnly, strMyTitle
    
End Sub