diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-07-08 12:46:34 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-07-08 12:46:34 +0200 |
| commit | 455cbc85ee6f2a4a3408f5e75e5d5f813d8b48fa (patch) | |
| tree | 4acd93e8aebb7219028a8ad297a4889d4a7b9357 /challenge-146 | |
| parent | 393d8f7b2cc5cb6fa63fd185b730a61173e452a4 (diff) | |
| download | perlweeklychallenge-club-455cbc85ee6f2a4a3408f5e75e5d5f813d8b48fa.tar.gz perlweeklychallenge-club-455cbc85ee6f2a4a3408f5e75e5d5f813d8b48fa.tar.bz2 perlweeklychallenge-club-455cbc85ee6f2a4a3408f5e75e5d5f813d8b48fa.zip | |
feat(challenge-146/lubos-kolouch/perl,python/): Challenge 146 LK Perl Python
Diffstat (limited to 'challenge-146')
| -rw-r--r-- | challenge-146/lubos-kolouch/perl/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-146/lubos-kolouch/perl/ch-2.pl | 22 | ||||
| -rw-r--r-- | challenge-146/lubos-kolouch/python/ch-1.py | 15 | ||||
| -rw-r--r-- | challenge-146/lubos-kolouch/python/ch-2.py | 19 |
4 files changed, 73 insertions, 0 deletions
diff --git a/challenge-146/lubos-kolouch/perl/ch-1.pl b/challenge-146/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..c14852dffa --- /dev/null +++ b/challenge-146/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,17 @@ +use strict; +use warnings; + +sub nth_prime { + my ($n) = @_; + my @primes = (2); + my $i = 3; + while ( @primes < $n ) { + if ( !grep { $i % $_ == 0 } @primes ) { + push @primes, $i; + } + $i += 2; + } + return $primes[-1]; +} + +print nth_prime(10001), "\n"; # Output: 104743 diff --git a/challenge-146/lubos-kolouch/perl/ch-2.pl b/challenge-146/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..3537ebcfc9 --- /dev/null +++ b/challenge-146/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +use Math::Fraction; + +sub find_ancestors { + my ($fraction) = @_; + my ( $num, $denom ) = split( '/', $fraction ); + my $parent = + $denom > $num + ? Math::Fraction->new( $num, $denom - $num ) + : Math::Fraction->new( $num - $denom, $denom ); + my $grandparent = + $parent->denominator > $parent->numerator + ? Math::Fraction->new( $parent->numerator, + $parent->denominator - $parent->numerator ) + : Math::Fraction->new( $parent->numerator - $parent->denominator, + $parent->denominator ); + return $parent->to_string, $grandparent->to_string; +} + +print join( ' and ', find_ancestors('3/5') ), "\n"; # Output: 3/2 and 1/2 +print join( ' and ', find_ancestors('4/3') ), "\n"; # Output: 1/3 and 1/2 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') |
