aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-01-11 17:56:41 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-01-11 17:56:41 +0000
commit08a76ad16ee62b2cbb2cda3508445047f2ff9cf1 (patch)
treeabc20eb4c1d6897474795ed4f8a2c0f52d37d1d7
parentb42dadbd4845c296030649fd61085055c699baba (diff)
downloadperlweeklychallenge-club-08a76ad16ee62b2cbb2cda3508445047f2ff9cf1.tar.gz
perlweeklychallenge-club-08a76ad16ee62b2cbb2cda3508445047f2ff9cf1.tar.bz2
perlweeklychallenge-club-08a76ad16ee62b2cbb2cda3508445047f2ff9cf1.zip
- Added guest contributions by Eric Cheung.
-rwxr-xr-xchallenge-147/eric-cheung/excel-vba/Challenge_147.xlsmbin0 -> 25600 bytes
-rwxr-xr-xchallenge-147/eric-cheung/excel-vba/ch-1.bas86
-rwxr-xr-xchallenge-147/eric-cheung/python/ch-2.py45
3 files changed, 131 insertions, 0 deletions
diff --git a/challenge-147/eric-cheung/excel-vba/Challenge_147.xlsm b/challenge-147/eric-cheung/excel-vba/Challenge_147.xlsm
new file mode 100755
index 0000000000..6f7427aff8
--- /dev/null
+++ b/challenge-147/eric-cheung/excel-vba/Challenge_147.xlsm
Binary files differ
diff --git a/challenge-147/eric-cheung/excel-vba/ch-1.bas b/challenge-147/eric-cheung/excel-vba/ch-1.bas
new file mode 100755
index 0000000000..558dd6656c
--- /dev/null
+++ b/challenge-147/eric-cheung/excel-vba/ch-1.bas
@@ -0,0 +1,86 @@
+Attribute VB_Name = "ModTask_01"
+Option Explicit
+
+Public Const strMyTitle As String = "Eric Cheung"
+
+Option Base 1
+Public nPrimeArr() As Long
+Public nPrimeCnt As Integer
+
+Function IsPrime(nInput As Integer) As Boolean
+
+ Dim nNumLoop As Integer
+
+ If nInput = 1 Then
+ IsPrime = False
+ Exit Function
+ End If
+
+ For nNumLoop = LBound(nPrimeArr) To UBound(nPrimeArr)
+ If nInput = nPrimeArr(nNumLoop) Then
+ IsPrime = True
+ Exit Function
+ ElseIf nInput Mod nPrimeArr(nNumLoop) = 0 Then
+ IsPrime = False
+ Exit Function
+ End If
+ Next nNumLoop
+
+ IsPrime = True
+
+End Function
+
+Function IsTruncatablePrime(nInput As Integer) As Boolean
+
+ If nInput = 0 Then
+ IsTruncatablePrime = False
+ Exit Function
+ ElseIf Not IsPrime(nInput) Then
+ IsTruncatablePrime = False
+ Exit Function
+ End If
+
+ nPrimeCnt = nPrimeCnt + 1
+ ReDim Preserve nPrimeArr(1 To nPrimeCnt)
+ nPrimeArr(nPrimeCnt) = nInput
+
+ If nInput < 10 Then
+ IsTruncatablePrime = True
+ Else
+ If Val(Mid(CStr(nInput), 2, 1)) = 0 Then
+ IsTruncatablePrime = False
+ Exit Function
+ End If
+ IsTruncatablePrime = IsTruncatablePrime(Val(Right(CStr(nInput), Len(CStr(nInput)) - 1)))
+ End If
+
+End Function
+
+Sub Task_01()
+
+ Const nTruncatablePrimeCntMax As Integer = 20
+
+ Dim nLoop As Integer, nTruncatablePrimeCnt As Integer
+ Dim strMsg As String
+
+ ReDim nPrimeArr(1 To 1)
+
+ nPrimeCnt = 1
+ nPrimeArr(1) = 2
+ nLoop = 3
+
+ nTruncatablePrimeCnt = 1
+
+ strMsg = "First " & nTruncatablePrimeCntMax & " left-truncatable prime numbers in base 10 are: " & 2
+
+ Do While (nTruncatablePrimeCnt < nTruncatablePrimeCntMax)
+ If IsTruncatablePrime(nLoop) Then
+ strMsg = strMsg & ", " & nLoop
+ nTruncatablePrimeCnt = nTruncatablePrimeCnt + 1
+ End If
+ nLoop = nLoop + 1
+ Loop
+
+ MsgBox strMsg, vbOKOnly, strMyTitle
+
+End Sub
diff --git a/challenge-147/eric-cheung/python/ch-2.py b/challenge-147/eric-cheung/python/ch-2.py
new file mode 100755
index 0000000000..6a716b2ddd
--- /dev/null
+++ b/challenge-147/eric-cheung/python/ch-2.py
@@ -0,0 +1,45 @@
+## Remarks
+## https://www.mathblog.dk/project-euler-44-smallest-pair-pentagonal-numbers/
+## https://radiusofcircle.blogspot.com/2016/06/problem-44-project-euler-solution-with-python.html
+## http://radiusofcircle.blogspot.com
+
+## Time Module for Calculating Execution Time
+import time
+
+## Time At The Start of Program Execution
+dStartTime = time.time()
+
+def is_pentagonal(nInput):
+ ## Function To Check If The Number is Pentagonal Number or Not
+ if (1 + (24 * nInput + 1) ** 0.5) % 6 == 0:
+ return True
+ return False
+
+## Flag To Check If The Number Is Found Or Not
+bFlag = True
+
+# While Loop Iterator
+nLoop = 1
+
+# While Loop
+while bFlag:
+ nNum_01 = nLoop * (3 * nLoop - 1) / 2
+ for nSubLoop in range(1, nLoop):
+ nNum_02 = nSubLoop * (3 * nSubLoop - 1) / 2
+ if is_pentagonal(nNum_01 + nNum_02) and is_pentagonal(nNum_01 - nNum_02):
+ print ("nLoop = " + str(nLoop) + ", nSubLoop = " + str(nSubLoop))
+ bFlag = False
+ break
+ nLoop = nLoop + 1
+
+# Time At The End of Program Execution
+dEndTime = time.time()
+
+# Printing The Total Time For Execution
+print ("Time Needed: " + str(dEndTime - dStartTime))
+
+
+## Result
+## nLoop = 2167, nSubLoop = 1020
+## Time Needed: 1.1174473762512207
+