From 01bd3ebebaeeda210d1a6c31168348252aa55e58 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 26 Oct 2021 09:34:48 +0100 Subject: - Added guest contributions by Eric Cheung. --- .../eric-cheung/excel-vba/Challenge_136.xlsm | Bin 0 -> 35288 bytes challenge-136/eric-cheung/excel-vba/ch-1.bas | 49 +++++++++++++ challenge-136/eric-cheung/excel-vba/ch-2.bas | 81 +++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100755 challenge-136/eric-cheung/excel-vba/Challenge_136.xlsm create mode 100755 challenge-136/eric-cheung/excel-vba/ch-1.bas create mode 100755 challenge-136/eric-cheung/excel-vba/ch-2.bas diff --git a/challenge-136/eric-cheung/excel-vba/Challenge_136.xlsm b/challenge-136/eric-cheung/excel-vba/Challenge_136.xlsm new file mode 100755 index 0000000000..60be8cd670 Binary files /dev/null and b/challenge-136/eric-cheung/excel-vba/Challenge_136.xlsm differ diff --git a/challenge-136/eric-cheung/excel-vba/ch-1.bas b/challenge-136/eric-cheung/excel-vba/ch-1.bas new file mode 100755 index 0000000000..fa539bf43b --- /dev/null +++ b/challenge-136/eric-cheung/excel-vba/ch-1.bas @@ -0,0 +1,49 @@ +Attribute VB_Name = "ModTask_01" +Option Explicit + +Public Const strMyTitle As String = "Eric Cheung" + +Function IsTwoFriendly(ByVal nInput_01 As Integer, nInput_02 As Integer) As Boolean + + Dim dPow As Double + Dim wsFunc As Object + + Set wsFunc = Application.WorksheetFunction + + dPow = Log(wsFunc.Gcd(nInput_01, nInput_02)) / Log(2) + + If dPow > Int(dPow) Then + IsTwoFriendly = False + Else + IsTwoFriendly = True + End If + + Set wsFunc = Nothing + +End Function + +Sub Task_01() + + '' Example 1 + '' Const nNum_01 As Integer = 8 + '' Const nNum_02 As Integer = 24 + + '' Example 2 + '' Const nNum_01 As Integer = 26 + '' Const nNum_02 As Integer = 39 + + '' Example 3 + Const nNum_01 As Integer = 4 + Const nNum_02 As Integer = 10 + + Dim strMsg As String + + If IsTwoFriendly(nNum_01, nNum_02) Then + strMsg = "1" + Else + strMsg = "0" + End If + + MsgBox strMsg, vbOKOnly, strMyTitle + +End Sub diff --git a/challenge-136/eric-cheung/excel-vba/ch-2.bas b/challenge-136/eric-cheung/excel-vba/ch-2.bas new file mode 100755 index 0000000000..c1c840ee69 --- /dev/null +++ b/challenge-136/eric-cheung/excel-vba/ch-2.bas @@ -0,0 +1,81 @@ +Attribute VB_Name = "ModTask_02" +Option Explicit + +Public arrFibonSeq() As Variant + +Sub GenFibonSeq(ByVal nNum As Integer) + + Dim nLoop As Integer, nSum As Integer + + nLoop = 2 + ReDim arrFibonSeq(1 To nLoop) + + arrFibonSeq(1) = 1 + arrFibonSeq(2) = 2 + + Do + nSum = arrFibonSeq(nLoop) + arrFibonSeq(nLoop - 1) + If nSum > nNum Then Exit Sub + nLoop = nLoop + 1 + ReDim Preserve arrFibonSeq(1 To nLoop) + arrFibonSeq(nLoop) = nSum + Loop Until nSum > nNum + +End Sub + +Sub Task_02() + + '' Const nInput As Integer = 16 '' Example 1 + '' Const nInput As Integer = 9 '' Example 2 + Const nInput As Integer = 15 '' Example 3 + + Dim nArrSize As Integer, nWayCount As Integer, nIndxLoop As Integer, nLastIndx As Integer, nLoopSum As Integer + Dim bFlag As Boolean + + If _ + nInput = 1 _ + Or nInput = 2 _ + Then + nWayCount = 1 + Else + nWayCount = 0 + + GenFibonSeq (nInput) + nArrSize = UBound(arrFibonSeq) - LBound(arrFibonSeq) + 1 + + nLastIndx = nArrSize + + Do While nLastIndx > 1 + nLoopSum = nInput + For nIndxLoop = nLastIndx To 1 Step -1 + If nLoopSum >= arrFibonSeq(nIndxLoop) Then + bFlag = False + + If _ + nLoopSum = arrFibonSeq(nIndxLoop) _ + And nIndxLoop < nArrSize _ + And nIndxLoop >= 3 _ + Then + bFlag = True + End If + + nLoopSum = nLoopSum - arrFibonSeq(nIndxLoop) + + If nLoopSum = 0 Then + nWayCount = nWayCount + 1 + If bFlag Then + nWayCount = nWayCount + 1 + End If + Exit For + End If + + End If + Next nIndxLoop + + nLastIndx = nLastIndx - 1 + Loop + End If + + MsgBox nWayCount, vbOKOnly, strMyTitle + +End Sub -- cgit