aboutsummaryrefslogtreecommitdiff
path: root/challenge-149/paulo-custodio/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-149/paulo-custodio/python')
-rw-r--r--challenge-149/paulo-custodio/python/ch-1.py36
-rw-r--r--challenge-149/paulo-custodio/python/ch-2.py39
2 files changed, 75 insertions, 0 deletions
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))