From 455cbc85ee6f2a4a3408f5e75e5d5f813d8b48fa Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 8 Jul 2023 12:46:34 +0200 Subject: feat(challenge-146/lubos-kolouch/perl,python/): Challenge 146 LK Perl Python --- challenge-146/lubos-kolouch/python/ch-1.py | 15 +++++++++++++++ challenge-146/lubos-kolouch/python/ch-2.py | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 challenge-146/lubos-kolouch/python/ch-1.py create mode 100644 challenge-146/lubos-kolouch/python/ch-2.py (limited to 'challenge-146/lubos-kolouch/python') 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') -- cgit