aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-06-16 14:26:20 +0100
committerGitHub <noreply@github.com>2022-06-16 14:26:20 +0100
commit07094b2c5ff58b46ab54497c2a80c5451705fac1 (patch)
tree7bec84698852cb0784ea9f67abbb3f95d8f73f44
parentaa64948dbbcb3a643b4f23c939f0b8f7167558c3 (diff)
parentc99d4e2e180ac519c405e7bb7f799eb479aa0cea (diff)
downloadperlweeklychallenge-club-07094b2c5ff58b46ab54497c2a80c5451705fac1.tar.gz
perlweeklychallenge-club-07094b2c5ff58b46ab54497c2a80c5451705fac1.tar.bz2
perlweeklychallenge-club-07094b2c5ff58b46ab54497c2a80c5451705fac1.zip
Merge pull request #6272 from mikedici/mdicicco-169
michael-dicicco java and python solutions
-rw-r--r--challenge-169/michael-dicicco/java/ch1.java54
-rw-r--r--challenge-169/michael-dicicco/java/ch2.java70
-rw-r--r--challenge-169/michael-dicicco/python/ch-1.py24
-rw-r--r--challenge-169/michael-dicicco/python/ch-2.py38
4 files changed, 186 insertions, 0 deletions
diff --git a/challenge-169/michael-dicicco/java/ch1.java b/challenge-169/michael-dicicco/java/ch1.java
new file mode 100644
index 0000000000..d9978ca311
--- /dev/null
+++ b/challenge-169/michael-dicicco/java/ch1.java
@@ -0,0 +1,54 @@
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ *
+ * @author mddicicco
+ */
+public class ch1 {
+
+ /**
+ * @param args the command line arguments
+ */
+ public static void main(String[] args) {
+ List<String> brilliants = new ArrayList<>();
+ for (long i = 4L; i < 300L; i++) {
+ if (isBrilliant(primeFactors(i))){
+ brilliants.add(Long.toString(i));
+ }
+ }
+ System.out.println(String.join(", ", brilliants));
+ }
+
+ public static Boolean isBrilliant(List<Long> factors) {
+ return (factors.size() == 2 && factors.get(0).toString().length() == factors.get(1).toString().length());
+ }
+
+ public static List<Long> primeFactors(long n) {
+ List<Long> output = new ArrayList<>();
+
+ // Print the number of 2s that divide n
+ while (n % 2L == 0L) {
+ output.add(2L);
+ n /= 2L;
+ }
+
+ // n must be odd at this point. So we can
+ // skip one element (Note i = i +2)
+ for (long i = 3L; i <= Math.sqrt(n); i += 2L) {
+ // While i divides n, print i and divide n
+ while (n % i == 0) {
+ output.add(i);
+ n /= i;
+ }
+ }
+
+ // This condition is to handle the case when
+ // n is a prime number greater than 2
+ if (n > 2L) {
+ output.add(n);
+ }
+ return output;
+ }
+
+}
diff --git a/challenge-169/michael-dicicco/java/ch2.java b/challenge-169/michael-dicicco/java/ch2.java
new file mode 100644
index 0000000000..94d53d318c
--- /dev/null
+++ b/challenge-169/michael-dicicco/java/ch2.java
@@ -0,0 +1,70 @@
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ *
+ * @author mddicicco
+ */
+public class ch2 {
+ static final double PRECISION = Math.pow(10,12);
+ public static void main(String[] args){
+
+ List<String> achilles_nums = new ArrayList<>();
+
+ for(long i = 72; i < 1801; i++){
+ if(isAchilles(primeFactors(i), i)){
+ achilles_nums.add(Long.toString(i));
+ }
+ }
+ System.out.println(String.join(", ", achilles_nums));
+ }
+ public static Boolean isAchilles(List<Long> factors, Long given){
+ for(Long factor : factors){
+ if(!(given % Math.pow(factor, 2) == 0))
+ return false;
+ }
+
+
+
+ for(long i = 2; i <= 9; i++){
+ double root = Math.round(nthRoot(given, i) * PRECISION)/PRECISION;
+
+ if (Math.floor(root) == root){
+
+ return false;
+ }
+ }
+ return true;
+
+ }
+ public static double nthRoot(double someNumber, double n){
+ return Math.pow(n, ((1.0 / n ) * (Math.log(someNumber) / Math.log(n))));
+ }
+
+ public static List<Long> primeFactors(long n) {
+ List<Long> output = new ArrayList<>();
+
+ // Print the number of 2s that divide n
+ while (n % 2L == 0L) {
+ output.add(2L);
+ n /= 2L;
+ }
+
+ // n must be odd at this point. So we can
+ // skip one element (Note i = i +2)
+ for (long i = 3L; i <= Math.sqrt(n); i += 2L) {
+ // While i divides n, print i and divide n
+ while (n % i == 0) {
+ output.add(i);
+ n /= i;
+ }
+ }
+
+ // This condition is to handle the case when
+ // n is a prime number greater than 2
+ if (n > 2L) {
+ output.add(n);
+ }
+ return output;
+ }
+}
diff --git a/challenge-169/michael-dicicco/python/ch-1.py b/challenge-169/michael-dicicco/python/ch-1.py
new file mode 100644
index 0000000000..102d3eaeb5
--- /dev/null
+++ b/challenge-169/michael-dicicco/python/ch-1.py
@@ -0,0 +1,24 @@
+def is_brilliant(factors):
+ """a brilliant number has only two prime factors of the same length"""
+ return len(factors) == 2 and len(str(int(factors[0]))) == len(str(int(factors[1])))
+
+
+def prime_factors(n):
+ output = []
+
+ while n % 2 == 0:
+ output.append(2)
+ n /= 2
+
+ for i in range(3, int(n ** 1 / 2 + 1), 2):
+ while n % i == 0:
+ output.append(i)
+ n /= i
+
+ if n > 2:
+ output.append(int(n))
+ return output
+
+
+if __name__ == '__main__':
+ print(", ".join([str(i) for i in range(4, 300) if is_brilliant(prime_factors(i))]))
diff --git a/challenge-169/michael-dicicco/python/ch-2.py b/challenge-169/michael-dicicco/python/ch-2.py
new file mode 100644
index 0000000000..8052d94e24
--- /dev/null
+++ b/challenge-169/michael-dicicco/python/ch-2.py
@@ -0,0 +1,38 @@
+def is_achilles(factors, given):
+ """Achilles was powerful but imperfect"""
+ for factor in factors:
+ """a powerful number is divisible by the squares of its prime factors"""
+ if not given % factor ** 2 == 0:
+ return False
+ for i in range(2, 10):
+ """a number is a perfect power if it has any integer roots"""
+ if nth_root(given, i) % 1 == 0:
+ return False
+ """the number is powerful and imperfect, like Achilles"""
+ return True
+
+
+def nth_root(some_number, n):
+ """raising a number to a fractional power of 1/n is the same as taking the nth root"""
+ return some_number ** (1 / n)
+
+
+def prime_factors(n):
+ output = []
+
+ while n % 2 == 0:
+ output.append(2)
+ n /= 2
+
+ for i in range(3, int(n ** 1 / 2 + 1), 2):
+ while n % i == 0:
+ output.append(i)
+ n /= i
+
+ if n > 2:
+ output.append(int(n))
+ return output
+
+
+if __name__ == '__main__':
+ print(", ".join([str(i) for i in range(72, 1801) if is_achilles(prime_factors(i), i)]))