diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-12-04 11:10:17 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-12-04 11:10:17 +0000 |
| commit | a5142150602b78793af374bf2e1e4c6f2644cca9 (patch) | |
| tree | 9da1aceb3918a764b5f9d5579a0de8567aea9300 /challenge-246/eric-cheung/python | |
| parent | 2fc5bc743f2ba88415e5d7c1d25be74c0d046ce5 (diff) | |
| download | perlweeklychallenge-club-a5142150602b78793af374bf2e1e4c6f2644cca9.tar.gz perlweeklychallenge-club-a5142150602b78793af374bf2e1e4c6f2644cca9.tar.bz2 perlweeklychallenge-club-a5142150602b78793af374bf2e1e4c6f2644cca9.zip | |
- Added solutions by Jan Krnavek.
- Added solutions by W. Luis Mochan.
- Added solutions by David Ferrone.
- Added solutions by Luca Ferrari.
Diffstat (limited to 'challenge-246/eric-cheung/python')
| -rwxr-xr-x | challenge-246/eric-cheung/python/ch-1.py | 17 | ||||
| -rwxr-xr-x | challenge-246/eric-cheung/python/ch-2.py | 71 |
2 files changed, 88 insertions, 0 deletions
diff --git a/challenge-246/eric-cheung/python/ch-1.py b/challenge-246/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..5ad578ad8b --- /dev/null +++ b/challenge-246/eric-cheung/python/ch-1.py @@ -0,0 +1,17 @@ +
+from random import randint
+
+nMaxNum = 49
+nNumChosen = 6
+
+arrInput = list(range(1, nMaxNum + 1))
+arrOuput = []
+
+for nLoop in range(nNumChosen):
+ nIndx = randint(0, nMaxNum - nLoop)
+ arrOuput.append(arrInput[nIndx])
+ del arrInput[nIndx]
+
+arrOuput.sort()
+
+print (arrOuput)
diff --git a/challenge-246/eric-cheung/python/ch-2.py b/challenge-246/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..efbbb54bbb --- /dev/null +++ b/challenge-246/eric-cheung/python/ch-2.py @@ -0,0 +1,71 @@ +
+## Ref.
+## https://math.libretexts.org/Courses/Mount_Royal_University/MATH_2150%3A_Higher_Arithmetic/5%3A_Diophantine_Equations/5.1%3A_Linear_Diophantine_Equations#:~:text=A%20Linear%20Diophantine%20equation%20(LDE,and%20y%20are%20unknown%20variables.
+## https://stackoverflow.com/questions/59168914/finding-solutions-to-diophantine
+
+from math import gcd
+import sys
+
+
+def IsConsecutiveEven (arrNum):
+ for nIndx in range(0, len(arrNum) - 1):
+ if arrNum[nIndx] % 2 == 0 and arrNum[nIndx + 1] % 2 == 0:
+ return nIndx
+ return -1
+
+
+def DotProduct (arrNum_01, arrNum_02):
+ return sum(arrLoop[0] * arrLoop[1] for arrLoop in zip(arrNum_01, arrNum_02))
+
+
+def ModifiedGCD (arrNum, arrParam):
+ if arrNum[1] == 0:
+ return [arrNum[0], 1, 0]
+
+ nX = 0
+ nY = 0
+
+ nDiv, nX, nY = ModifiedGCD([arrNum[1], arrNum[0] % arrNum[1]], [nX, nY])
+ return [nDiv, nY, nX - nY * (arrNum[0] // arrNum[1])]
+
+arrInput = [1, 1, 2, 3, 5] ## Example 1
+## arrInput = [4, 2, 4, 5, 7] ## Example 2
+## arrInput = [4, 1, 2, -3, 8] ## Example 3
+
+
+## print (ModifiedGCD(arrInput[0:2], [0, 0]))
+## print (ModifiedGCD([47, 30], [0, 0]))
+
+nConsecutiveEvenIndx = IsConsecutiveEven (arrInput)
+if nConsecutiveEvenIndx > -1:
+ arrContainOdd = [nIndx for nIndx in range(nConsecutiveEvenIndx + 2, len(arrInput)) if arrInput[nIndx] % 2 == 1]
+ if len(arrContainOdd) > 0:
+ print (False)
+ sys.exit()
+
+nGCD = gcd (arrInput[0], arrInput[1])
+if arrInput[2] % nGCD != 0:
+ print (False)
+ sys.exit()
+
+arrParam = []
+
+## ==== To Be Further Fine Tune ====
+if arrInput[2] == arrInput[0] + arrInput[1]:
+ arrParam.append(1)
+ arrParam.append(1)
+elif arrInput[2] == arrInput[0] - 2 * arrInput[1]:
+ arrParam.append(1)
+ arrParam.append(-2)
+## ==== To Be Further Fine Tune ====
+
+if len(arrParam) == 0:
+ print (False)
+ sys.exit()
+
+for nIndx in range(3, 5):
+ if arrInput[nIndx] != DotProduct(arrParam, arrInput[nIndx - 2 : nIndx]):
+ print (False)
+ sys.exit()
+
+print (True)
|
