aboutsummaryrefslogtreecommitdiff
path: root/challenge-023/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorDave Jacoby <jacoby.david@gmail.com>2021-12-07 17:08:53 -0500
committerDave Jacoby <jacoby.david@gmail.com>2021-12-07 17:08:53 -0500
commitf0230b0a58c8d2a63ba172c0c04ac75e5c80e0d4 (patch)
treea7162c0c01b282684f159edc35a2f940a8a77e38 /challenge-023/paulo-custodio/python/ch-2.py
parent038f05c9243b83b2b654559b5005506bc6a3fc2b (diff)
parent59dd7ccf422a3b10f31e3ad2cc9a2704ce6b40e0 (diff)
downloadperlweeklychallenge-club-f0230b0a58c8d2a63ba172c0c04ac75e5c80e0d4.tar.gz
perlweeklychallenge-club-f0230b0a58c8d2a63ba172c0c04ac75e5c80e0d4.tar.bz2
perlweeklychallenge-club-f0230b0a58c8d2a63ba172c0c04ac75e5c80e0d4.zip
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-023/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-023/paulo-custodio/python/ch-2.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-023/paulo-custodio/python/ch-2.py b/challenge-023/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..4438d1af8a
--- /dev/null
+++ b/challenge-023/paulo-custodio/python/ch-2.py
@@ -0,0 +1,38 @@
+#!/usr/bin/python3
+
+# Challenge 023
+#
+# Task #2
+# Create a script that prints Prime Decomposition of a given number. The prime
+# decomposition of a number is defined as a list of prime numbers which when
+# all multiplied together, are equal to that number. For example, the Prime
+# decomposition of 228 is 2,2,3,19 as 228 = 2 * 2 * 3 * 19.
+
+import sys
+from primePy import primes
+
+def next_prime(n):
+ if n <= 1:
+ return 2
+ else:
+ n += 1
+ while not primes.check(n):
+ n += 1
+ return n
+
+def prime_decomposition(n):
+ if n<2:
+ return [n]
+
+ f = []
+ p = 2
+ while n>1:
+ if n%p == 0:
+ f.append(p)
+ n //= p
+ else:
+ p = next_prime(p)
+ return f
+
+f = prime_decomposition(int(sys.argv[1]))
+print(", ".join([str(x) for x in f]))