From d5ee7c5fa4fe77b9ea632d61d68f37800a569312 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Thu, 27 Jan 2022 19:01:24 +0000 Subject: - Added guest contributions by Eric Cheung. --- .../eric-cheung/excel-vba/Challenge_149.xlsm | Bin 0 -> 34543 bytes challenge-149/eric-cheung/excel-vba/ch-1.bas | 64 +++++++++++++++++++++ challenge-149/eric-cheung/excel-vba/ch-2.bas | 56 ++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100755 challenge-149/eric-cheung/excel-vba/Challenge_149.xlsm create mode 100755 challenge-149/eric-cheung/excel-vba/ch-1.bas create mode 100755 challenge-149/eric-cheung/excel-vba/ch-2.bas diff --git a/challenge-149/eric-cheung/excel-vba/Challenge_149.xlsm b/challenge-149/eric-cheung/excel-vba/Challenge_149.xlsm new file mode 100755 index 0000000000..82c320d844 Binary files /dev/null and b/challenge-149/eric-cheung/excel-vba/Challenge_149.xlsm differ diff --git a/challenge-149/eric-cheung/excel-vba/ch-1.bas b/challenge-149/eric-cheung/excel-vba/ch-1.bas new file mode 100755 index 0000000000..19d58d26fc --- /dev/null +++ b/challenge-149/eric-cheung/excel-vba/ch-1.bas @@ -0,0 +1,64 @@ +Attribute VB_Name = "ModTask_01" +Option Explicit + +Public Const strMyTitle As String = "Eric Cheung" + +Function GetSumOfDigit(ByVal nNum As Integer) As Integer + + Dim nSubLoop As Integer + + For nSubLoop = 1 To Len(CStr(nNum)) + GetSumOfDigit = GetSumOfDigit + CInt(Mid(CStr(nNum), nSubLoop, 1)) + Next nSubLoop + +End Function + +Function IsInArray(nValToFind As Variant, nArr As Variant) As Boolean + + '' Credit: + '' https://wellsr.com/vba/2016/excel/check-if-value-is-in-array-vba/ + + Dim nElem As Variant + + IsInArray = False + + For Each nElem In nArr + If nElem = nValToFind Then + IsInArray = True + Exit Function + End If + Next nElem + +End Function + +Sub Task_01() + + Const MyNumMax As Integer = 20 + + Dim nLoop As Integer, nCount As Integer + Dim strMsg As String + Dim nFibArr(1 To MyNumMax) As Integer + + nFibArr(1) = 0: nFibArr(2) = 1 + + For nLoop = 3 To MyNumMax + nFibArr(nLoop) = nFibArr(nLoop - 2) + nFibArr(nLoop - 1) + Next nLoop + + nLoop = 0 + nCount = 0 + + Do While (nCount < MyNumMax) + If IsInArray(GetSumOfDigit(nLoop), nFibArr) Then + nCount = nCount + 1 + If strMsg <> "" Then + strMsg = strMsg & ", " + End If + strMsg = strMsg & CStr(nLoop) + End If + nLoop = nLoop + 1 + Loop + + MsgBox strMsg, vbOKOnly, strMyTitle + +End Sub diff --git a/challenge-149/eric-cheung/excel-vba/ch-2.bas b/challenge-149/eric-cheung/excel-vba/ch-2.bas new file mode 100755 index 0000000000..85c58180c0 --- /dev/null +++ b/challenge-149/eric-cheung/excel-vba/ch-2.bas @@ -0,0 +1,56 @@ +Attribute VB_Name = "ModTask_02" +Option Explicit + +Function GetSqRoot(ByVal nNum As Integer) As String + + Const nNumMax As Integer = 22 + Dim strSqRootArr(1 To nNumMax) As String + + If nNum < 2 Or nNum > nNumMax Then + GetSqRoot = "" + Exit Function + End If + + strSqRootArr(1) = "" + strSqRootArr(2) = "1" + strSqRootArr(3) = "1" + strSqRootArr(4) = "3201" + strSqRootArr(5) = "4301" + strSqRootArr(6) = "452013" + strSqRootArr(7) = "6250341" + strSqRootArr(8) = "47302651" + strSqRootArr(9) = "823146570" + strSqRootArr(10) = "9814072356" + strSqRootArr(11) = "A8701245369" + strSqRootArr(12) = "B8750A649321" + strSqRootArr(13) = "CBA504216873" + strSqRootArr(14) = "DC71B30685A924" + strSqRootArr(15) = "EDAC93B24658701" + strSqRootArr(16) = "FED5B39A42706C81" + strSqRootArr(17) = "GFED5A31C6B79802" + strSqRootArr(18) = "HGF80ADC53712EB649" + strSqRootArr(19) = "IHGFD3408C6E715A2B9" + strSqRootArr(20) = "JIHG03DAC457BFE96281" + strSqRootArr(22) = "LKJIG5D14B9032FHAC867E" + + GetSqRoot = strSqRootArr(nNum) + +End Function + +Sub Task_02() + + '' Credits: + '' https://www.quora.com/How-do-you-square-a-number-that-isnt-in-base-ten + '' https://github.com/manwar/perlweeklychallenge-club/blob/master/challenge-149/abigail/bash/ch-2.sh + + Dim strMsg As String + + '' strMsg = GetSqRoot(2) + '' strMsg = GetSqRoot(4) + '' strMsg = GetSqRoot(10) + strMsg = GetSqRoot(12) + + MsgBox strMsg, vbOKOnly, strMyTitle + +End Sub + -- cgit