diff options
| -rw-r--r-- | challenge-159/lubos-kolouch/perl/ch-1.pl | 31 | ||||
| -rw-r--r-- | challenge-159/lubos-kolouch/perl/ch-2.pl | 32 | ||||
| -rw-r--r-- | challenge-159/lubos-kolouch/python/ch-1.py | 20 | ||||
| -rw-r--r-- | challenge-159/lubos-kolouch/python/ch-2.py | 20 |
4 files changed, 103 insertions, 0 deletions
diff --git a/challenge-159/lubos-kolouch/perl/ch-1.pl b/challenge-159/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..5404f62720 --- /dev/null +++ b/challenge-159/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,31 @@ +package main; +use strict; +use warnings; + +sub farey_sequence { + my $n = shift; + + # I just adapted the Python algorithm from the wiki """ + + my ( $a, $b, $c, $d ) = ( 0, 1, 1, $n ); + + my @solution; + + push @solution, "$a/$b"; + while ( $c <= $n ) { + my $k = int( ( $n + $b ) / $d ); + my ( $a, $b, $c, $d ) = ( $c, $d, $k * $c - $a, $k * $d - $b ); + push @solution, "$a/$b"; + } + + return join( ", ", @solution ); +} + +use Test::More; + +is( farey_sequence(5), + "0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1" ); + +done_testing; + +1; diff --git a/challenge-159/lubos-kolouch/perl/ch-2.pl b/challenge-159/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..ceb68e2918 --- /dev/null +++ b/challenge-159/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,32 @@ +package main; +use strict; +use warnings; +use Math::Prime::Util qw/factor/; + +sub get_moebius_nr { + my $what = shift; + + my @factors = factor($what); + + my %fact_hash; + + for my $fact (@factors) { + $fact_hash{$fact} += 1; + if ( $fact_hash{$fact} == 2 ) { + return 0; + } + } + + return 1 if scalar @factors % 2 == 0; + return -1; + +} + +use Test::More; + +is( get_moebius_nr(5), -1 ); +is( get_moebius_nr(10), 1 ); +is( get_moebius_nr(20), 0 ); + +done_testing; +1; diff --git a/challenge-159/lubos-kolouch/python/ch-1.py b/challenge-159/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..cdfcf94d85 --- /dev/null +++ b/challenge-159/lubos-kolouch/python/ch-1.py @@ -0,0 +1,20 @@ +""" Challenge 159 Task 1""" + + +def farey_sequence(n: int, descending: bool = False) -> str: + """Print the n'th Farey sequence. Allow for either ascending or descending.""" + (a, b, c, d) = (0, 1, 1, n) + + solution = [] + if descending: + (a, c) = (1, n - 1) + solution.append(f"{a}/{b}") + while (c <= n and not descending) or (a > 0 and descending): + k = (n + b) // d + (a, b, c, d) = (c, d, k * c - a, k * d - b) + solution.append(f"{a}/{b}") + + return ", ".join(solution) + + +assert farey_sequence(5) == "0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1" diff --git a/challenge-159/lubos-kolouch/python/ch-2.py b/challenge-159/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..b5ab8e6d88 --- /dev/null +++ b/challenge-159/lubos-kolouch/python/ch-2.py @@ -0,0 +1,20 @@ +""" Challenge 159 Task 2""" +from sympy.ntheory import factorint + + +def get_moebius_nr(what: int): + """Get the moebius nr""" + factors = factorint(what) + + if any(value > 1 for value in factors.values()): + return 0 + + if len(factors) % 2 == 0: + return 1 + + return -1 + + +assert get_moebius_nr(5) == -1 +assert get_moebius_nr(10) == 1 +assert get_moebius_nr(20) == 0 |
