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

Public Const strMyTitle As String = "Eric Cheung"

Option Base 1
Public nPrimeArr() As Long

Function IsPrime(nInput As Integer) As Boolean

    Dim nNumLoop As Integer
    
    For nNumLoop = LBound(nPrimeArr) To UBound(nPrimeArr)
        If nInput Mod nPrimeArr(nNumLoop) = 0 Then
            IsPrime = False
            Exit Function
        End If
    Next nNumLoop

    IsPrime = True
    
End Function

Sub Task_01()

    Const nPrimeCntMax As Integer = 10001

    Dim nLoop As Integer, nPrimeCnt As Integer
    
    ReDim nPrimeArr(1 To 1)
    
    nPrimeCnt = 1
    nPrimeArr(1) = 2
    nLoop = 3

    Do While (nPrimeCnt < nPrimeCntMax)
        If IsPrime(nLoop) Then
            nPrimeCnt = nPrimeCnt + 1
            ReDim Preserve nPrimeArr(1 To nPrimeCnt)
            nPrimeArr(nPrimeCnt) = nLoop
        End If
        nLoop = nLoop + 1
    Loop

    MsgBox nPrimeArr(nPrimeCnt), vbOKOnly, strMyTitle

End Sub