aboutsummaryrefslogtreecommitdiff
path: root/challenge-146/lubos-kolouch/python
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-146/lubos-kolouch/python')
-rw-r--r--challenge-146/lubos-kolouch/python/ch-1.py15
-rw-r--r--challenge-146/lubos-kolouch/python/ch-2.py19
2 files changed, 34 insertions, 0 deletions
diff --git a/challenge-146/lubos-kolouch/python/ch-1.py b/challenge-146/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..3191b3e2e4
--- /dev/null
+++ b/challenge-146/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+def nth_prime(n):
+ primes = [2]
+ i = 3
+ while len(primes) < n:
+ if all(i % p > 0 for p in primes):
+ primes.append(i)
+ i += 2
+ return primes[-1]
+
+
+print(nth_prime(10001)) # Output: 104743
diff --git a/challenge-146/lubos-kolouch/python/ch-2.py b/challenge-146/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..0cfdb31445
--- /dev/null
+++ b/challenge-146/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,19 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from fractions import Fraction
+
+
+def find_ancestors(fraction):
+ num, denom = map(int, fraction.split("/"))
+ parent = Fraction(num, denom - num) if denom > num else Fraction(num - denom, denom)
+ grandparent = (
+ Fraction(parent.numerator, parent.denominator - parent.numerator)
+ if parent.denominator > parent.numerator
+ else Fraction(parent.numerator - parent.denominator, parent.denominator)
+ )
+ return str(parent), str(grandparent)
+
+
+print(find_ancestors("3/5")) # Output: ('3/2', '1/2')
+print(find_ancestors("4/3")) # Output: ('1/3', '1/2')