aboutsummaryrefslogtreecommitdiff
path: root/challenge-150/paulo-custodio/python/ch-2.py
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-09-27 11:59:06 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-09-27 11:59:06 +0100
commit23bc575e1d957ae33105c1508dd8b6721a9f5331 (patch)
tree87ef6a4ba84591f90c974bb2ab089bfdd64bd9a9 /challenge-150/paulo-custodio/python/ch-2.py
parent7de6f2107b0b6852a3210340e7e4d31de11df7a6 (diff)
downloadperlweeklychallenge-club-23bc575e1d957ae33105c1508dd8b6721a9f5331.tar.gz
perlweeklychallenge-club-23bc575e1d957ae33105c1508dd8b6721a9f5331.tar.bz2
perlweeklychallenge-club-23bc575e1d957ae33105c1508dd8b6721a9f5331.zip
Add Python solution to challenge 150
Diffstat (limited to 'challenge-150/paulo-custodio/python/ch-2.py')
-rw-r--r--challenge-150/paulo-custodio/python/ch-2.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/challenge-150/paulo-custodio/python/ch-2.py b/challenge-150/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..bdfb6bc4ae
--- /dev/null
+++ b/challenge-150/paulo-custodio/python/ch-2.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+
+# Challenge 150
+#
+# TASK #2 > Square-free Integer
+# Submitted by: Mohammad S Anwar
+# Write a script to generate all square-free integers <= 500.
+#
+# In mathematics, a square-free integer (or squarefree integer) is an integer
+# which is divisible by no perfect square other than 1. That is, its prime
+# factorization has exactly one factor for each prime that appears in it. For
+# example, 10 = 2 x 5 is square-free, but 18 = 2 x 3 x 3 is not, because 18 is
+# divisible by 9 = 3**2.
+#
+# Example
+# The smallest positive square-free integers are
+# 1, 2, 3, 5, 6, 7, 10, 11, 13, 14, 15, 17, 19, 21, 22, 23, 26, 29, 30, ...
+
+from sympy import factorint
+
+def is_squarefree(n):
+ factors = factorint(n)
+ for k in factors.values():
+ if k > 1:
+ return False
+ return True
+
+out = [n for n in range(1, 501) if is_squarefree(n)]
+print(", ".join(map(str, out)))