diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-05-11 00:06:50 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-05-11 00:06:50 +0100 |
| commit | cefef1500af294313e93db4d852e7316030ef7d0 (patch) | |
| tree | ec0aecafb6c33755c1ab7aa7a35bfbd63922ff51 /challenge-164/eric-cheung/python | |
| parent | c8c0600811f83f67b914310c6d3bf4fdc025a807 (diff) | |
| download | perlweeklychallenge-club-cefef1500af294313e93db4d852e7316030ef7d0.tar.gz perlweeklychallenge-club-cefef1500af294313e93db4d852e7316030ef7d0.tar.bz2 perlweeklychallenge-club-cefef1500af294313e93db4d852e7316030ef7d0.zip | |
- Added guest contributions by Eric Cheung.
Diffstat (limited to 'challenge-164/eric-cheung/python')
| -rwxr-xr-x | challenge-164/eric-cheung/python/ch-1.py | 32 | ||||
| -rwxr-xr-x | challenge-164/eric-cheung/python/ch-2.py | 33 |
2 files changed, 65 insertions, 0 deletions
diff --git a/challenge-164/eric-cheung/python/ch-1.py b/challenge-164/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..92040209e1 --- /dev/null +++ b/challenge-164/eric-cheung/python/ch-1.py @@ -0,0 +1,32 @@ +
+arrPrime = []
+arrIsPalindromePrime = []
+
+def IsPrime(nInput):
+ for primeLoop in arrPrime:
+ ## print (primeLoop)
+ if nInput % primeLoop == 0:
+ return False
+
+ return True
+
+def IsPalindrome(strInput):
+ if strInput == strInput[::-1]:
+ return True
+
+ return False
+
+arrPrime.append(2)
+arrPrime.append(3)
+
+arrIsPalindromePrime.append(2)
+arrIsPalindromePrime.append(3)
+
+for nLoop in range(5, 1000):
+ if IsPrime(nLoop):
+ arrPrime.append(nLoop)
+ if IsPalindrome(str(nLoop)):
+ arrIsPalindromePrime.append(nLoop)
+
+## print (arrPrime)
+print (arrIsPalindromePrime)
diff --git a/challenge-164/eric-cheung/python/ch-2.py b/challenge-164/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..1f63da88b1 --- /dev/null +++ b/challenge-164/eric-cheung/python/ch-2.py @@ -0,0 +1,33 @@ +
+## Reference:
+## https://www.javatpoint.com/python-program-to-check-if-the-given-number-is-happy-number
+
+nDiv = 10
+
+def GetDigitSumSq(strInput):
+ nSum = 0
+ for nDigit in range(0, len(strInput)):
+ nSum = nSum + int(strInput[nDigit]) * int(strInput[nDigit])
+
+ return nSum
+
+def IsHappyNumber(nInput):
+ if nInput == 1:
+ return True
+ elif nInput == 4:
+ return False
+ else:
+ return IsHappyNumber(GetDigitSumSq(str(nInput)))
+
+nLoop = 1
+nCount = 0
+arrHappyNum = []
+
+while nCount < 8:
+ if IsHappyNumber(nLoop):
+ arrHappyNum.append(nLoop)
+ nCount = nCount + 1
+
+ nLoop = nLoop + 1
+
+print (arrHappyNum)
|
