diff options
| author | Luis Mochan <mochan@fis.unam.mx> | 2022-04-05 18:46:15 -0500 |
|---|---|---|
| committer | Luis Mochan <mochan@fis.unam.mx> | 2022-04-05 18:46:15 -0500 |
| commit | 39af713a6911dc620727be9060a71a588016f87d (patch) | |
| tree | 9109f6c860d3040b4e8b717257bb902714e294f9 /challenge-159 | |
| parent | b2ad01584238b6bf7603faeb2748d93d431345a6 (diff) | |
| download | perlweeklychallenge-club-39af713a6911dc620727be9060a71a588016f87d.tar.gz perlweeklychallenge-club-39af713a6911dc620727be9060a71a588016f87d.tar.bz2 perlweeklychallenge-club-39af713a6911dc620727be9060a71a588016f87d.zip | |
Solve PWC 159
Diffstat (limited to 'challenge-159')
| -rw-r--r-- | challenge-159/wlmb/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-159/wlmb/perl/ch-1.pl | 22 | ||||
| -rwxr-xr-x | challenge-159/wlmb/perl/ch-2.pl | 21 |
3 files changed, 44 insertions, 0 deletions
diff --git a/challenge-159/wlmb/blog.txt b/challenge-159/wlmb/blog.txt new file mode 100644 index 0000000000..28e9e6bb1e --- /dev/null +++ b/challenge-159/wlmb/blog.txt @@ -0,0 +1 @@ +https://wlmb.github.io/2022/04/05/PWC159/ diff --git a/challenge-159/wlmb/perl/ch-1.pl b/challenge-159/wlmb/perl/ch-1.pl new file mode 100755 index 0000000000..7925b31bbd --- /dev/null +++ b/challenge-159/wlmb/perl/ch-1.pl @@ -0,0 +1,22 @@ +#!/usr/bin/env perl +# Perl weekly challenge 159 +# Task 1: Farey sequence +# +# See https://wlmb.github.io/2022/04/05/PWC159/#task-1-farey-sequence +use v5.12; +use warnings; +use POSIX qw(floor); +use Text::Wrap qw(wrap $columns $break); +die "Usage: ./ch-1.pl N1 [N2... ]\n to print Farey sequences of order N1, N2..." unless @ARGV; +for(@ARGV){ + my @farey=([0,1],[1,$_]); + while($farey[-1][1]!=1){ + my ($a,$b,$c,$d)=(@{$farey[-2]}, @{$farey[-1]}); + my $k=floor(($_+$b)/$d); + push @farey, [$k*$c-$a, $k*$d-$b]; + } + $columns=62; $break=qr/\s/; + say "Input: $_\n",wrap("", " ", "Output: ", + join ", ", + map {"$_->[0]/$_->[1]"} @farey); +} diff --git a/challenge-159/wlmb/perl/ch-2.pl b/challenge-159/wlmb/perl/ch-2.pl new file mode 100755 index 0000000000..49f8067707 --- /dev/null +++ b/challenge-159/wlmb/perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/bin/env perl +# Perl weekly challenge 159 +# Task 2: Moebius number +# +# See https://wlmb.github.io/2022/04/05/PWC159/#task-2-moebius-number +use v5.12; +use warnings; +use PDL; +use PDL::NiceSlice; +die "Usage: ./ch-2.pl N1 [N2... ]\n to find the Moebius numbers of N1, N2..." unless @ARGV; +for(@ARGV){ + my $sieve=ones($_+1); # Eratosthenes sieve + $sieve(0:1).=0; + $sieve($_**2:-1:$_).=0 for(2..sqrt($_)); # 1=prime, 0=nonprime + my $primes=$sieve->xvals->where($sieve); # primes + my $factors=$primes->where($_%$primes==0); # prime factors + my $prod=$factors->prodover; # product of factors, once each + my $square_free=$prod==$_; + my $parity=$factors->nelem%2?-1:1; + say "Input: $_ Output: ", $square_free?$parity:0; +} |
