diff options
| author | LapVeesh <rabbiveesh@gmail.com> | 2019-08-11 23:46:33 +0300 |
|---|---|---|
| committer | LapVeesh <rabbiveesh@gmail.com> | 2019-08-11 23:46:33 +0300 |
| commit | 090af051b19abda6d8c10b7cafe0eb8a119bd9ec (patch) | |
| tree | cab5da5dd2bd585ba90cdbae529a88ae346b7851 | |
| parent | 9cd51ef37c07ca4e36440943b6e72e92e51e667a (diff) | |
| download | perlweeklychallenge-club-090af051b19abda6d8c10b7cafe0eb8a119bd9ec.tar.gz perlweeklychallenge-club-090af051b19abda6d8c10b7cafe0eb8a119bd9ec.tar.bz2 perlweeklychallenge-club-090af051b19abda6d8c10b7cafe0eb8a119bd9ec.zip | |
Added answers to challenge 20. Many regexes
| -rw-r--r-- | challenge-020/veesh-goldman/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-020/veesh-goldman/perl5/ch-01.sh | 3 | ||||
| -rwxr-xr-x | challenge-020/veesh-goldman/perl5/ch-02.pl | 24 | ||||
| -rw-r--r-- | challenge-020/veesh-goldman/perl5/t/factors.t | 17 |
4 files changed, 45 insertions, 0 deletions
diff --git a/challenge-020/veesh-goldman/blog.txt b/challenge-020/veesh-goldman/blog.txt new file mode 100644 index 0000000000..b66597e216 --- /dev/null +++ b/challenge-020/veesh-goldman/blog.txt @@ -0,0 +1 @@ +http://blogs.perl.org/users/veesh/2019/08/solving-two-problems.html diff --git a/challenge-020/veesh-goldman/perl5/ch-01.sh b/challenge-020/veesh-goldman/perl5/ch-01.sh new file mode 100755 index 0000000000..14c1ada7e3 --- /dev/null +++ b/challenge-020/veesh-goldman/perl5/ch-01.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +perl -pE'split / /, s/(.) \g1*/$& /gx' diff --git a/challenge-020/veesh-goldman/perl5/ch-02.pl b/challenge-020/veesh-goldman/perl5/ch-02.pl new file mode 100755 index 0000000000..e5ef309883 --- /dev/null +++ b/challenge-020/veesh-goldman/perl5/ch-02.pl @@ -0,0 +1,24 @@ +#! /usr/bin/env perl +use v5.22; + +sub sum_of_proper_divisors { + #start with one, because that is always a divisor + my $sum = 1; + (1 x shift) =~ /^ (..+) \g1+ $ (?{ $sum += length $1 }) ./xg; + return $sum +} + +sub has_amicable { + my $start = shift; + #sometimes the sum of the divisors IS the number. But that's not amicable + return 0 if sum_of_proper_divisors($start) == $start; + return $start == sum_of_proper_divisors( sum_of_proper_divisors($start) ) +} + + +my $num; +while (1) { + last if has_amicable(++$num); +} + +say "$num, ${\sum_of_proper_divisors($num)} is your pair" diff --git a/challenge-020/veesh-goldman/perl5/t/factors.t b/challenge-020/veesh-goldman/perl5/t/factors.t new file mode 100644 index 0000000000..7467115a68 --- /dev/null +++ b/challenge-020/veesh-goldman/perl5/t/factors.t @@ -0,0 +1,17 @@ +use Test2::V0; + + +sub sum_factors { + my $expansion = 1 x shift; + my $sum = 1; + $expansion =~ /^(..+) \g1+ $ (?{ $sum += length $1 }) ./xg; + + return $sum +} + +is sum_factors(4), 3; +is sum_factors(220), 284; +is sum_factors(284), 220; + + +done_testing; |
