aboutsummaryrefslogtreecommitdiff
path: root/challenge-133/eric-cheung/excel-vba/ch-1.bas
blob: afb640a377f6c68c7e93046e9ebc863cb7302080 (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
Attribute VB_Name = "ModTask_01"
Option Explicit
Public Const strMyTitle As String = "Eric Cheung"

Function FindIntDivTwo(nNum As Double) As Long

    FindIntDivTwo = Int(nNum / 2)

End Function

Function FindIntSqrRoot(nInput As Long) As Long
    
    Dim nTemp As Long
    
    nTemp = FindIntDivTwo(CDbl(nInput))
    
    If nTemp = 0 Then
        FindIntSqrRoot = nInput
        Exit Function
    End If
    
    FindIntSqrRoot = FindIntDivTwo(CDbl(nTemp + nInput / nTemp))

    Do While FindIntSqrRoot < nTemp
        nTemp = FindIntSqrRoot
        FindIntSqrRoot = FindIntDivTwo(CDbl(nTemp + nInput / nTemp))
    Loop
    
End Function

Sub Task_01()

    ''Remarks
    ''https://theweeklychallenge.org/blog/perl-weekly-challenge-133/
    ''https://en.wikipedia.org/wiki/Integer_square_root
    ''https://www.geeksforgeeks.org/left-shift-right-shift-operators-c-cpp/

    Dim strMsg As String
    Dim nIntSqr As Long

    '' nIntSqr = 10 '' Example: 1
    '' nIntSqr = 27 '' Example: 2
    '' nIntSqr = 85 '' Example: 3
    nIntSqr = 101 '' Example: 4

    strMsg = "Integer Square Root of " & nIntSqr & " is: " + CStr(FindIntSqrRoot(nIntSqr))

    MsgBox strMsg, vbOKOnly, strMyTitle

End Sub