aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-09-28 19:38:42 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-09-28 19:38:42 +0100
commit1e40fcc88e800e74dc8bc1291020a614b362cb61 (patch)
treec89e9c55dc057e10e3d31f3205593bb042d9ecb1
parent341287fb7780a3c5ee45ec0521ef99d11dbdbcce (diff)
downloadperlweeklychallenge-club-1e40fcc88e800e74dc8bc1291020a614b362cb61.tar.gz
perlweeklychallenge-club-1e40fcc88e800e74dc8bc1291020a614b362cb61.tar.bz2
perlweeklychallenge-club-1e40fcc88e800e74dc8bc1291020a614b362cb61.zip
Add Python solution to challenge 156
-rw-r--r--challenge-156/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-156/paulo-custodio/python/ch-1.py35
-rw-r--r--challenge-156/paulo-custodio/python/ch-2.py59
3 files changed, 95 insertions, 1 deletions
diff --git a/challenge-156/paulo-custodio/perl/ch-2.pl b/challenge-156/paulo-custodio/perl/ch-2.pl
index d16e291ea5..d77b690bbf 100644
--- a/challenge-156/paulo-custodio/perl/ch-2.pl
+++ b/challenge-156/paulo-custodio/perl/ch-2.pl
@@ -2,7 +2,7 @@
# Challenge 156
#
-# TASK #2 › Weird Number
+# TASK #2 > Weird Number
# Submitted by: Mohammad S Anwar
# You are given number, $n > 0.
#
diff --git a/challenge-156/paulo-custodio/python/ch-1.py b/challenge-156/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..dd16bf4390
--- /dev/null
+++ b/challenge-156/paulo-custodio/python/ch-1.py
@@ -0,0 +1,35 @@
+#!/usr/bin/env python3
+
+# Challenge 156
+#
+# TASK #1 > Pernicious Numbers
+# Submitted by: Mohammad S Anwar
+# Write a script to permute first 10 Pernicious Numbers.
+#
+# A pernicious number is a positive integer which has prime number of ones in
+# its binary representation.
+#
+# The first pernicious number is 3 since binary representation of 3 = (11)
+# and 1 + 1 = 2, which is a prime.
+#
+# Expected Output
+# 3, 5, 6, 7, 9, 10, 11, 12, 13, 14
+
+from sympy import isprime
+
+def is_pernicious(n):
+ bin_n = bin(n)[2:] # Convert to binary and remove the '0b' prefix
+ ones = bin_n.count('1')
+ is_pernicious = isprime(ones)
+ return is_pernicious
+
+def first_pernicious(num):
+ out = []
+ n = 0
+ while len(out) < num:
+ if is_pernicious(n):
+ out.append(n)
+ n += 1
+ return out
+
+print(", ".join(map(str, first_pernicious(10))))
diff --git a/challenge-156/paulo-custodio/python/ch-2.py b/challenge-156/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..b198001956
--- /dev/null
+++ b/challenge-156/paulo-custodio/python/ch-2.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+
+# Challenge 156
+#
+# TASK #2 > Weird Number
+# Submitted by: Mohammad S Anwar
+# You are given number, $n > 0.
+#
+# Write a script to find out if the given number is a Weird Number.
+#
+# According to Wikipedia, it is defined as:
+#
+# The sum of the proper divisors (divisors including 1 but not itself) of the
+# number is greater than the number, but no subset of those divisors sums to
+# the number itself.
+#
+# Example 1:
+# Input: $n = 12
+# Output: 0
+#
+# Since the proper divisors of 12 are 1, 2, 3, 4, and 6, which sum to 16; but
+# 2 + 4 + 6 = 12.
+# Example 2:
+# Input: $n = 70
+# Output: 1
+#
+# As the proper divisors of 70 are 1, 2, 5, 7, 10, 14, and 35; these sum to
+# 74, but no subset of these sums to 70.
+
+import sys
+from itertools import combinations
+from math import isqrt
+
+def divisors(n):
+ divs = set()
+ for i in range(1, isqrt(n) + 1):
+ if n % i == 0:
+ divs.add(i)
+ divs.add(n // i)
+ return divs
+
+def proper_divisors(n):
+ return [d for d in divisors(n) if d != n]
+
+def check_no_subset_sums_n(n, div):
+ for k in range(1, len(div) + 1):
+ for combin in combinations(div, k):
+ if sum(combin) == n:
+ return False
+ return True
+
+def is_weird(n):
+ div = proper_divisors(n)
+ if sum(div) <= n:
+ return False
+ return check_no_subset_sums_n(n, div)
+
+n = int(sys.argv[1])
+print(1 if is_weird(n) else 0)