aboutsummaryrefslogtreecommitdiff
path: root/challenge-148
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-09-27 10:55:44 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-09-27 10:55:44 +0100
commitdfd896477be36c3a279b8a1134e6ef3113b0f5b2 (patch)
tree5625a405fa502ecd12922ceb443334b551ac6920 /challenge-148
parent4a2fee41a38b28f1e17b26768caf000185fa5350 (diff)
downloadperlweeklychallenge-club-dfd896477be36c3a279b8a1134e6ef3113b0f5b2.tar.gz
perlweeklychallenge-club-dfd896477be36c3a279b8a1134e6ef3113b0f5b2.tar.bz2
perlweeklychallenge-club-dfd896477be36c3a279b8a1134e6ef3113b0f5b2.zip
Add Python solution to challenge 148
Diffstat (limited to 'challenge-148')
-rw-r--r--challenge-148/paulo-custodio/perl/ch-1.pl4
-rw-r--r--challenge-148/paulo-custodio/perl/ch-2.pl48
-rw-r--r--challenge-148/paulo-custodio/python/ch-1.py26
-rw-r--r--challenge-148/paulo-custodio/python/ch-2.py50
-rw-r--r--challenge-148/paulo-custodio/t/test-2.yaml2
5 files changed, 113 insertions, 17 deletions
diff --git a/challenge-148/paulo-custodio/perl/ch-1.pl b/challenge-148/paulo-custodio/perl/ch-1.pl
index 2a0ce52919..9dd13818c2 100644
--- a/challenge-148/paulo-custodio/perl/ch-1.pl
+++ b/challenge-148/paulo-custodio/perl/ch-1.pl
@@ -2,11 +2,11 @@
# Challenge 148
#
-# TASK #1 › Eban Numbers
+# TASK #1 > Eban Numbers
# Submitted by: Mohammad S Anwar
# Write a script to generate all Eban Numbers <= 100.
#
-# An Eban number is a number that has no letter ‘e’ in it when the number
+# An Eban number is a number that has no letter 'e' in it when the number
# is spelled in English (American or British).
#
# Example
diff --git a/challenge-148/paulo-custodio/perl/ch-2.pl b/challenge-148/paulo-custodio/perl/ch-2.pl
index eceb0dc533..2bd9173d42 100644
--- a/challenge-148/paulo-custodio/perl/ch-2.pl
+++ b/challenge-148/paulo-custodio/perl/ch-2.pl
@@ -2,7 +2,7 @@
# Challenge 148
#
-# TASK #2 › Cardano Triplets
+# TASK #2 > Cardano Triplets
# Submitted by: Mohammad S Anwar
# Write a script to generate first 5 Cardano Triplets.
#
@@ -15,27 +15,47 @@
# (2,1,5) is the first Cardano Triplets.
use Modern::Perl;
+use List::Util 'sum';
-my $limit = 100;
my @triplets = cardano_triplets(5);
-for (0..4) {
- say "(", join(",", @{$triplets[$_]}), ")";
+for (@triplets) {
+ say "(", join(",", @$_), ")";
}
-
sub cardano_triplets {
- my($n) = @_;
+ my($num) = @_;
- my @found;
- for my $a (1..$limit) {
- for my $b (1..$limit) {
- for my $c (1..$limit) {
- if (8*($a**3)+15*($a**2)+6*$a-27*($b**2)*$c==1) {
- push @found, [$a, $b, $c];
- }
+ my @triplets;
+ my $K = 0;
+ while (@triplets < $num*2) { # we are not sure in which order they are generated
+ my $A = 2+3*$K;
+ my $T = ($K+1)**2 * (8*$K+5);
+ my @divs = divisors($T);
+ for my $div (@divs) {
+ if (int(sqrt($div)) == sqrt($div)) {
+ my $B = sqrt($div);
+ my $C = $T / $B**2;
+ push @triplets, [$A, $B, $C];
}
}
+ @triplets = sort {sum(@$a) <=> sum(@$b)} @triplets;
+
+ $K++;
}
- return @found;
+ return @triplets[0..$num-1];
+}
+
+sub divisors {
+ my($n) = @_;
+ my @divs1;
+ my @divs2;
+ for my $k (1 .. $n) {
+ last if @divs2 && $k >= $divs2[0];
+ if ($n % $k == 0) {
+ push @divs1, $k;
+ unshift @divs2, $n/$k;
+ }
+ }
+ return (@divs1, @divs2);
}
diff --git a/challenge-148/paulo-custodio/python/ch-1.py b/challenge-148/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..9417ec6343
--- /dev/null
+++ b/challenge-148/paulo-custodio/python/ch-1.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python3
+
+# Challenge 148
+#
+# TASK #1 > Eban Numbers
+# Submitted by: Mohammad S Anwar
+# Write a script to generate all Eban Numbers <= 100.
+#
+# An Eban number is a number that has no letter 'e' in it when the number
+# is spelled in English (American or British).
+#
+# Example
+# 2, 4, 6, 30, 32 are the first 5 Eban numbers.
+
+from num2words import num2words
+
+def is_eban(n):
+ en = num2words(n)
+ return 'e' not in en
+
+out = []
+for n in range(1, 101):
+ if is_eban(n):
+ out.append(n)
+
+print(", ".join(map(str, out)))
diff --git a/challenge-148/paulo-custodio/python/ch-2.py b/challenge-148/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..73114e1555
--- /dev/null
+++ b/challenge-148/paulo-custodio/python/ch-2.py
@@ -0,0 +1,50 @@
+#!/usr/bin/env python3
+
+# Challenge 148
+#
+# TASK #2 > Cardano Triplets
+# Submitted by: Mohammad S Anwar
+# Write a script to generate first 5 Cardano Triplets.
+#
+# A triplet of positive integers (a,b,c) is called a Cardano Triplet if it
+# satisfies the below condition.
+#
+# Cardano Triplets
+#
+# Example
+# (2,1,5) is the first Cardano Triplets.
+
+from math import isqrt
+from itertools import chain
+
+def cardano_triplets(num):
+ triplets = []
+ K = 0
+ while len(triplets) < num * 2: # we are not sure in which order they are generated
+ A = 2 + 3 * K
+ T = (K + 1) ** 2 * (8 * K + 5)
+ divs = divisors(T)
+ for div in divs:
+ if isqrt(div) ** 2 == div:
+ B = isqrt(div)
+ C = T // (B ** 2)
+ triplets.append((A, B, C))
+ triplets.sort(key=lambda x: sum(x))
+ K += 1
+
+ return triplets[:num]
+
+def divisors(n):
+ divs1 = []
+ divs2 = []
+ for k in range(1, n + 1):
+ if divs2 and k >= divs2[0]:
+ break
+ if n % k == 0:
+ divs1.append(k)
+ divs2.insert(0, n // k)
+ return list(chain(divs1, divs2))
+
+triplets = cardano_triplets(5)
+for triplet in triplets:
+ print(f"({','.join(map(str, triplet))})")
diff --git a/challenge-148/paulo-custodio/t/test-2.yaml b/challenge-148/paulo-custodio/t/test-2.yaml
index eb8432b926..c2b0ba83bf 100644
--- a/challenge-148/paulo-custodio/t/test-2.yaml
+++ b/challenge-148/paulo-custodio/t/test-2.yaml
@@ -4,7 +4,7 @@
input:
output: |
|(2,1,5)
- |(5,1,52)
|(5,2,13)
|(8,3,21)
|(11,4,29)
+ |(14,5,37)