aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-09-27 11:31:34 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-09-27 11:31:34 +0100
commit7de6f2107b0b6852a3210340e7e4d31de11df7a6 (patch)
tree26bd978c15e4493e8496da66d2b450060b980b32
parentdfd896477be36c3a279b8a1134e6ef3113b0f5b2 (diff)
downloadperlweeklychallenge-club-7de6f2107b0b6852a3210340e7e4d31de11df7a6.tar.gz
perlweeklychallenge-club-7de6f2107b0b6852a3210340e7e4d31de11df7a6.tar.bz2
perlweeklychallenge-club-7de6f2107b0b6852a3210340e7e4d31de11df7a6.zip
Add Python solution to challenge 149
-rw-r--r--challenge-149/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-149/paulo-custodio/perl/ch-2.pl4
-rw-r--r--challenge-149/paulo-custodio/python/ch-1.py36
-rw-r--r--challenge-149/paulo-custodio/python/ch-2.py39
4 files changed, 78 insertions, 3 deletions
diff --git a/challenge-149/paulo-custodio/perl/ch-1.pl b/challenge-149/paulo-custodio/perl/ch-1.pl
index 5f4c74432f..48994f7185 100644
--- a/challenge-149/paulo-custodio/perl/ch-1.pl
+++ b/challenge-149/paulo-custodio/perl/ch-1.pl
@@ -2,7 +2,7 @@
# Challenge 149
#
-# TASK #1 › Fibonacci Digit Sum
+# TASK #1 > Fibonacci Digit Sum
# Submitted by: Roger Bell_West
# Given an input $N, generate the first $N numbers for which the sum of their
# digits is a Fibonacci number.
diff --git a/challenge-149/paulo-custodio/perl/ch-2.pl b/challenge-149/paulo-custodio/perl/ch-2.pl
index 3c99608c44..29ffbec4df 100644
--- a/challenge-149/paulo-custodio/perl/ch-2.pl
+++ b/challenge-149/paulo-custodio/perl/ch-2.pl
@@ -2,10 +2,10 @@
# Challenge 149
#
-# TASK #2 › Largest Square
+# TASK #2 > Largest Square
# Submitted by: Roger Bell_West
# Given a number base, derive the largest perfect square with no repeated
-# digits and return it as a string. (For base>10, use ‘A’..‘Z’.)
+# digits and return it as a string. (For base>10, use 'A'..'Z'.)
#
# Example:
# f(2)="1"
diff --git a/challenge-149/paulo-custodio/python/ch-1.py b/challenge-149/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..f3bd54f69d
--- /dev/null
+++ b/challenge-149/paulo-custodio/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+
+# Challenge 149
+#
+# TASK #1 > Fibonacci Digit Sum
+# Submitted by: Roger Bell_West
+# Given an input $N, generate the first $N numbers for which the sum of their
+# digits is a Fibonacci number.
+#
+# Example
+# f(20)=[0, 1, 2, 3, 5, 8, 10, 11, 12, 14, 17, 20, 21, 23, 26, 30, 32, 35, 41, 44]
+
+import sys
+from functools import reduce
+
+def is_fibonacci(n):
+ # Function to check if a number is a Fibonacci number
+ a, b = 0, 1
+ while a < n:
+ a, b = b, a + b
+ return a == n
+
+def sum_of_digits_is_fibonacci(n):
+ digits = [int(d) for d in str(n)]
+ total = sum(digits)
+ return is_fibonacci(total)
+
+count = int(sys.argv[1])
+out = []
+n = 0
+while len(out) < count:
+ if sum_of_digits_is_fibonacci(n):
+ out.append(n)
+ n += 1
+
+print(", ".join(map(str, out)))
diff --git a/challenge-149/paulo-custodio/python/ch-2.py b/challenge-149/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..b5c900633d
--- /dev/null
+++ b/challenge-149/paulo-custodio/python/ch-2.py
@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+# Challenge 149
+#
+# TASK #2 > Largest Square
+# Submitted by: Roger Bell_West
+# Given a number base, derive the largest perfect square with no repeated
+# digits and return it as a string. (For base>10, use 'A'..'Z'.)
+#
+# Example:
+# f(2)="1"
+# f(4)="3201"
+# f(10)="9814072356"
+# f(12)="B8750A649321"
+
+import sys
+import math
+from itertools import permutations
+
+def largest_perfect_square(base):
+ digits = [str(i) for i in range(10)] + [chr(i) for i in range(ord('A'), ord('A') + 26)]
+ digits = digits[:base]
+
+ max_num = 0
+ max_str = "0"
+
+ for permu in permutations(digits, base):
+ str_num = ''.join(permu)
+ str_num = str_num.lstrip('0') or '0'
+ num = int(str_num, base)
+
+ if math.isqrt(num) ** 2 == num: # is perfect square
+ if num > max_num:
+ max_num, max_str = num, str_num
+
+ return max_str
+
+base = int(sys.argv[1])
+print(largest_perfect_square(base))