From cfb5851065ef019a1c2415bc376f255a8de11f58 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 4 Jan 2022 21:53:56 +0000 Subject: - Added guest contributions by Eric Cheung. --- .../eric-cheung/excel-vba/Challenge_146.xlsm | Bin 0 -> 32721 bytes challenge-146/eric-cheung/excel-vba/ch-1.bas | 47 +++++++++++++++++++++ challenge-146/eric-cheung/excel-vba/ch-2.bas | 45 ++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100755 challenge-146/eric-cheung/excel-vba/Challenge_146.xlsm create mode 100755 challenge-146/eric-cheung/excel-vba/ch-1.bas create mode 100755 challenge-146/eric-cheung/excel-vba/ch-2.bas diff --git a/challenge-146/eric-cheung/excel-vba/Challenge_146.xlsm b/challenge-146/eric-cheung/excel-vba/Challenge_146.xlsm new file mode 100755 index 0000000000..c4e8ff0908 Binary files /dev/null and b/challenge-146/eric-cheung/excel-vba/Challenge_146.xlsm differ diff --git a/challenge-146/eric-cheung/excel-vba/ch-1.bas b/challenge-146/eric-cheung/excel-vba/ch-1.bas new file mode 100755 index 0000000000..d67a6d4137 --- /dev/null +++ b/challenge-146/eric-cheung/excel-vba/ch-1.bas @@ -0,0 +1,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 diff --git a/challenge-146/eric-cheung/excel-vba/ch-2.bas b/challenge-146/eric-cheung/excel-vba/ch-2.bas new file mode 100755 index 0000000000..aa7c7d16ca --- /dev/null +++ b/challenge-146/eric-cheung/excel-vba/ch-2.bas @@ -0,0 +1,45 @@ +Attribute VB_Name = "ModTask_02" +Option Explicit + +Sub FindCuriousFractionTree(ByVal nNumerator As Integer, ByVal nDenominator As Integer, ByRef nParentNumerator As Integer, ByRef nParentDenominator As Integer) + + If nNumerator > nDenominator Then + nParentNumerator = nNumerator - nDenominator + nParentDenominator = nDenominator + ElseIf nNumerator = nDenominator Then + nParentNumerator = nNumerator + nParentDenominator = nDenominator + Else + nParentNumerator = nNumerator + nParentDenominator = nDenominator - nNumerator + End If + +End Sub + +Sub Task_02() + + ''Credit: https://gdaymath.com/lessons/fractions/5-3-a-curious-fraction-tree/ + + '' Example 1 + '' Const nNumeratorInput As Integer = 3 + '' Const nDenominatorInput As Integer = 5 + + '' Example 2 + Const nNumeratorInput As Integer = 4 + Const nDenominatorInput As Integer = 3 + + Dim strMsg As String + Dim nNumeratorInputParent As Integer, nDenominatorInputParent As Integer + Dim nNumeratorInputGrandParent As Integer, nDenominatorInputGrandParent As Integer + + FindCuriousFractionTree nNumeratorInput, nDenominatorInput, nNumeratorInputParent, nDenominatorInputParent + FindCuriousFractionTree nNumeratorInputParent, nDenominatorInputParent, nNumeratorInputGrandParent, nDenominatorInputGrandParent + + strMsg = "Parent: " & nNumeratorInputParent & " / " & nDenominatorInputParent + strMsg = strMsg & vbNewLine + strMsg = strMsg & "GrandParent: " & nNumeratorInputGrandParent & " / " & nDenominatorInputGrandParent + + MsgBox strMsg, vbOKOnly, strMyTitle + +End Sub + -- cgit