diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-03-15 23:46:13 +0000 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-03-15 23:46:13 +0000 |
| commit | 081071c9a5eb941107c40373971d46f700d1421d (patch) | |
| tree | 7715f0935ecfe55f63cb44cbf779badecbedb4f6 /challenge-156/eric-cheung/python | |
| parent | fc270b51265576837e204e0f05de7a644aa23243 (diff) | |
| download | perlweeklychallenge-club-081071c9a5eb941107c40373971d46f700d1421d.tar.gz perlweeklychallenge-club-081071c9a5eb941107c40373971d46f700d1421d.tar.bz2 perlweeklychallenge-club-081071c9a5eb941107c40373971d46f700d1421d.zip | |
- Added guest contributions by Eric Cheung.
Diffstat (limited to 'challenge-156/eric-cheung/python')
| -rwxr-xr-x | challenge-156/eric-cheung/python/ch-1.py | 34 | ||||
| -rwxr-xr-x | challenge-156/eric-cheung/python/ch-2.py | 97 |
2 files changed, 131 insertions, 0 deletions
diff --git a/challenge-156/eric-cheung/python/ch-1.py b/challenge-156/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..f03068bbac --- /dev/null +++ b/challenge-156/eric-cheung/python/ch-1.py @@ -0,0 +1,34 @@ +## Remarks
+## https://www.geeksforgeeks.org/pernicious-number/
+## Python program to print first nNum Pernicious Numbers
+
+## Function to Check Prime Number
+def isPrime(nInput):
+ if nInput < 2:
+ return False
+
+ for nSubLoop in range(2, nInput):
+ if not nInput % nSubLoop:
+ return False
+
+ return True
+
+
+# Prints First nNum Pernicious Numbers
+def printPernicious(nNum):
+
+ nLoop, nCount = 1, 0
+
+ while nCount < nNum:
+ # "bin(nLoop).count('1')" Count No. of ones in Binary Representation
+ if (isPrime(bin(nLoop).count('1'))):
+ print(nLoop, end = ' ')
+ nCount = nCount + 1
+
+ nLoop = nLoop + 1
+
+## Driver Code
+nNum = 10
+printPernicious(nNum)
+
+## This Code is Contributed by Ansu Kumari
diff --git a/challenge-156/eric-cheung/python/ch-2.py b/challenge-156/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..970a05d6f1 --- /dev/null +++ b/challenge-156/eric-cheung/python/ch-2.py @@ -0,0 +1,97 @@ +## Remarks
+## https://www.geeksforgeeks.org/weird-number/
+
+## Python 3 Program to Check if the Number is Weird or not
+from math import sqrt
+
+## Code to Find all the Factors of the Number Excluding the Number itself
+def factors(n):
+
+ ## Vector to Store the Factors
+ v = []
+ v.append(1)
+
+ ## Note that this Loop runs till sqrt(n)
+ for i in range(2, int(sqrt(n)) + 1, 1):
+ ## if the value of i is a factor
+ if (n % i == 0):
+ v.append(i);
+
+ ## Condition to Check the Divisor is not the Number itself
+ if (int(n / i) != i):
+ v.append(int(n / i))
+
+ # return the vector
+ return v
+
+## Function to check if the number is Abundant or not
+def checkAbundant(n):
+ sum = 0
+
+ ## Find the Divisors Using Function
+ v = factors(n)
+
+ ## Sum All the Factors
+ for i in range(len(v)):
+ sum = sum + v[i]
+
+ ## Check for Abundant or Not
+ if (sum > n):
+ return True
+
+ return False
+
+## Function to Check if the Number is Semi-Perfect or not
+def checkSemiPerfect(n):
+ ## find the divisors
+ v = factors(n)
+
+ ## sorting the vector
+ v.sort(reverse = False)
+ r = len(v)
+
+ ## subset to check if no is semiperfect
+ subset = [[0 for i in range(n + 1)] for j in range(r + 1)]
+
+ ## initialising 1st column to true
+ for i in range(r + 1):
+ subset[i][0] = True
+
+ ## Initialing 1st Row Except Zero Position to 0
+ for i in range(1, n + 1):
+ subset[0][i] = False
+
+ ## Loop to Find Whether the Number is Semiperfect
+ for i in range(1, r + 1):
+ for j in range(1, n + 1):
+
+ ## Calculation to check if the Number can be made by Summation of Divisors
+ if (j < v[i - 1]):
+ subset[i][j] = subset[i - 1][j]
+ else:
+ subset[i][j] = (subset[i - 1][j] or subset[i - 1][j - v[i - 1]])
+
+ ## If not possible to make the Number by any combination of Divisors
+ if ((subset[r][n]) == 0):
+ return False
+
+ return True
+
+## Function to check for Weird or Not
+def checkweird(n):
+ if (checkAbundant(n) and not checkSemiPerfect(n)):
+ return True
+
+ return False
+
+# Driver Code
+if __name__ == '__main__':
+ ## n = 12 ## Example 1:
+ n = 70 ## Example 2:
+
+ if (checkweird(n)):
+ print("Weird Number")
+ else:
+ print("Not Weird Number")
+
+## This code is contributed by Surendra_Gangwar
|
