From 86ddf7b64e4859fc0b00eda0cb7cb0bdb62d12ad Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Wed, 22 Dec 2021 03:46:28 +0000 Subject: - Added guest contributions by Eric Cheung. --- .../eric-cheung/excel-vba/Challenge_144.xlsm | Bin 0 -> 36923 bytes challenge-144/eric-cheung/excel-vba/ch-1.bas | 76 ++++++++++++++++++ challenge-144/eric-cheung/excel-vba/ch-2.bas | 87 +++++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100755 challenge-144/eric-cheung/excel-vba/Challenge_144.xlsm create mode 100755 challenge-144/eric-cheung/excel-vba/ch-1.bas create mode 100755 challenge-144/eric-cheung/excel-vba/ch-2.bas diff --git a/challenge-144/eric-cheung/excel-vba/Challenge_144.xlsm b/challenge-144/eric-cheung/excel-vba/Challenge_144.xlsm new file mode 100755 index 0000000000..9afa059c40 Binary files /dev/null and b/challenge-144/eric-cheung/excel-vba/Challenge_144.xlsm differ diff --git a/challenge-144/eric-cheung/excel-vba/ch-1.bas b/challenge-144/eric-cheung/excel-vba/ch-1.bas new file mode 100755 index 0000000000..68ed30b1ef --- /dev/null +++ b/challenge-144/eric-cheung/excel-vba/ch-1.bas @@ -0,0 +1,76 @@ +Attribute VB_Name = "ModTask_01" +Option Explicit + +Public Const strMyTitle As String = "Eric Cheung" + +Function IsPrime(nInput As Integer) As Boolean + + Dim nLoop As Integer + + If nInput = 1 Then + IsPrime = False + Exit Function + End If + + 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 IsSemiPrime(nInput As Integer) As Boolean + + Dim nPrimeLoop As Integer + Dim bByPassLoop As Boolean + + For nPrimeLoop = 2 To nInput - 2 + + bByPassLoop = False + + If nInput Mod nPrimeLoop > 0 Then + bByPassLoop = True + End If + + If Not bByPassLoop And Not IsPrime(nPrimeLoop) Then + bByPassLoop = True + End If + + If Not bByPassLoop And Not IsPrime(nInput / nPrimeLoop) Then + bByPassLoop = True + End If + + If Not bByPassLoop Then + IsSemiPrime = True + Exit Function + End If + + Next nPrimeLoop + + IsSemiPrime = False + +End Function + +Sub Task_01() + + Dim strMsg As String + Dim nMainLoop As Integer + + For nMainLoop = 2 To 100 + If IsSemiPrime(nMainLoop) Then + If strMsg <> "" Then + strMsg = strMsg & ", " + End If + strMsg = strMsg & nMainLoop + End If + Next nMainLoop + + MsgBox strMsg, vbOKOnly, strMyTitle + +End Sub + + diff --git a/challenge-144/eric-cheung/excel-vba/ch-2.bas b/challenge-144/eric-cheung/excel-vba/ch-2.bas new file mode 100755 index 0000000000..08e1f84f0e --- /dev/null +++ b/challenge-144/eric-cheung/excel-vba/ch-2.bas @@ -0,0 +1,87 @@ +Attribute VB_Name = "ModTask_02" +Option Explicit +Public nNumArr() As Integer + +Function IsUniqComb(nInput As Integer) As Boolean + + Dim nSubLoop_01 As Integer, nSubLoop_02 As Integer + Dim bOccur As Boolean + + bOccur = False + IsUniqComb = True + + For nSubLoop_01 = LBound(nNumArr) To UBound(nNumArr) - 1 + For nSubLoop_02 = nSubLoop_01 + 1 To UBound(nNumArr) + If nNumArr(nSubLoop_01) + nNumArr(nSubLoop_02) = nInput Then + If Not bOccur Then + bOccur = True + Else + IsUniqComb = False + Exit Function + End If + End If + Next nSubLoop_02 + Next nSubLoop_01 + +End Function + +Sub Task_02() + + '' Example 1 + '' Const nNum_01 As Integer = 1 + '' Const nNum_02 As Integer = 2 + + '' Example 2 + '' Const nNum_01 As Integer = 2 + '' Const nNum_02 As Integer = 3 + + '' Example 3 + Const nNum_01 As Integer = 2 + Const nNum_02 As Integer = 5 + + Const nNumMax As Integer = 10 + + Dim strMsg As String + + Dim nLoop_01 As Integer, nLoop_02 As Integer, nLoopCnt As Integer + Dim nLastNum As Integer, nTempNum As Integer + + ReDim nNumArr(1 To 3) + + nNumArr(1) = nNum_01: nNumArr(2) = nNum_02: nNumArr(3) = nNum_01 + nNum_02: nLastNum = nNumArr(3) + + nLoopCnt = 3 + + Do While (nLoopCnt < nNumMax) + + nTempNum = 9999 + For nLoop_01 = LBound(nNumArr) To UBound(nNumArr) - 1 + For nLoop_02 = nLoop_01 + 1 To UBound(nNumArr) + If _ + nNumArr(nLoop_01) + nNumArr(nLoop_02) > nLastNum _ + And nNumArr(nLoop_01) + nNumArr(nLoop_02) < nTempNum _ + Then + nTempNum = nNumArr(nLoop_01) + nNumArr(nLoop_02) + End If + Next nLoop_02 + Next nLoop_01 + + If IsUniqComb(nTempNum) Then + nLoopCnt = nLoopCnt + 1 + ReDim Preserve nNumArr(1 To nLoopCnt) + nNumArr(nLoopCnt) = nTempNum + End If + + nLastNum = nTempNum + Loop + + For nLoop_01 = LBound(nNumArr) To UBound(nNumArr) + If strMsg <> "" Then + strMsg = strMsg & ", " + End If + strMsg = strMsg & nNumArr(nLoop_01) + Next nLoop_01 + + MsgBox strMsg, vbOKOnly, strMyTitle + +End Sub -- cgit