aboutsummaryrefslogtreecommitdiff
path: root/challenge-139/eric-cheung/excel-vba/ch-2.bas
blob: 1262f0557bac31f93780fa68a8c8007f619bfd7c (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
Attribute VB_Name = "ModTask_02"
Option Explicit

Function IsPrime(nInput As Integer) As Boolean

    Dim nLoop As Integer
    
    For nLoop = 2 To Int(Sqr(nInput))
        If nInput Mod nLoop = 0 Then
            IsPrime = False
            Exit Function
        End If
    Next nLoop
    
    IsPrime = True
    
End Function

Function FindPeriodPrime(nInput As Integer) As Integer

    Dim nTemp As Integer, nSubTemp As Integer, nLoop As Integer
    
    FindPeriodPrime = 0
    nTemp = 1
    
    For nLoop = 1 To nInput - 1
        nTemp = (10 * nTemp) Mod nInput
    Next nLoop
    
    nSubTemp = nTemp
    
    Do
        nTemp = (10 * nTemp) Mod nInput
        FindPeriodPrime = FindPeriodPrime + 1
    Loop While nSubTemp <> nTemp

End Function

Sub Task_02()

    '' Credit: https://rosettacode.org/wiki/Long_primes#Java
    
    Const nLongPrimeMaxCount As Integer = 5

    Dim strMsg As String
    Dim nLongPrimeLoopCnt As Integer
    Dim nPrimeLoop As Integer
    
    nLongPrimeLoopCnt = 0
    nPrimeLoop = 7 '' First Long Prime Number
    
    Do
        If IsPrime(nPrimeLoop) Then
            If FindPeriodPrime(nPrimeLoop) = nPrimeLoop - 1 Then
                If strMsg <> "" Then
                    strMsg = strMsg & ", "
                End If
                strMsg = strMsg & nPrimeLoop
                nLongPrimeLoopCnt = nLongPrimeLoopCnt + 1
            End If
        End If
        
        nPrimeLoop = nPrimeLoop + 2
    Loop While nLongPrimeLoopCnt < nLongPrimeMaxCount
    
    strMsg = "First " & nLongPrimeMaxCount & " Long Primes are: " & strMsg

    MsgBox strMsg, vbOKOnly, strMyTitle
    
End Sub