aboutsummaryrefslogtreecommitdiff
path: root/challenge-169/sgreen/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-169/sgreen/python')
-rwxr-xr-xchallenge-169/sgreen/python/ch-1.py54
-rwxr-xr-xchallenge-169/sgreen/python/ch-2.py56
2 files changed, 110 insertions, 0 deletions
diff --git a/challenge-169/sgreen/python/ch-1.py b/challenge-169/sgreen/python/ch-1.py
new file mode 100755
index 0000000000..dee277497c
--- /dev/null
+++ b/challenge-169/sgreen/python/ch-1.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python3
+
+import math
+
+
+def is_prime(number):
+ '''Return true or false if the number is a prime'''
+ if number < 2:
+ return False
+
+ for i in range(2, int(math.sqrt(number)) + 1):
+ if number % i == 0:
+ return False
+
+ # It's a prime
+ return True
+
+
+def get_bril_nums(l):
+ '''Get all primes of length l'''
+
+ primes = []
+ for i in range(10**(l-1), 10**l):
+ if is_prime(i):
+ primes.append(i)
+
+ # Calculate all brilliant numbers
+ bril_nums = []
+ for i, v1 in enumerate(primes):
+ for v2 in primes[i:]:
+ bril_nums.append(v1*v2)
+
+ # Return the sorted list
+ bril_nums.sort()
+ return bril_nums
+
+
+def main():
+ '''Find the first twenty brilliant numbers'''
+ l = 1
+ bril_nums = []
+
+ # Keep increasing the length of primes until we have at least twenty
+ # solutions
+ while len(bril_nums) < 20:
+ bril_nums.extend(get_bril_nums(l))
+ l += 1
+
+ # Print the first 20 numbers
+ print(*bril_nums[0:20], sep=', ')
+
+
+if __name__ == '__main__':
+ main()
diff --git a/challenge-169/sgreen/python/ch-2.py b/challenge-169/sgreen/python/ch-2.py
new file mode 100755
index 0000000000..3cd0323f3a
--- /dev/null
+++ b/challenge-169/sgreen/python/ch-2.py
@@ -0,0 +1,56 @@
+#!/usr/bin/env python3
+
+def is_achilles_number(num):
+ '''Determine if this number is an achilles numbers'''
+
+ # Get the prime factors of the number
+ factors = {}
+
+ i = 2
+ while num > 1:
+ if num % i == 0:
+ num /= i
+ if i in factors:
+ factors[i] += 1
+ else:
+ factors[i] = 1
+ else:
+ i += 1
+
+ # If there is only one prime factor, it is not an achilles number
+ if len(factors) == 1:
+ return False
+
+ # Get a list of unique powers
+ powers = set(factors.values())
+
+ # It's also not an achilles number if any of the powers were 1, or the
+ # powers are all the same (a perfect sqaure)
+ if 1 in powers or len(powers) == 1:
+ return False
+
+ # Finally, the greatest common divisor of these numbers has to be one
+ for i in range(2, min(powers)+1):
+ if all(x % i == 0 for x in powers):
+ return False
+
+ return True
+
+
+def main():
+ '''Find the first 20 achilles numbers'''
+ solutions = []
+ num = 2
+
+ # Get the first 20 achilles numbers
+ while len(solutions) < 20:
+ if is_achilles_number(num):
+ solutions.append(num)
+ num += 1
+
+ # Print the list
+ print(*solutions, sep=', ')
+
+
+if __name__ == '__main__':
+ main()