diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2019-08-11 23:28:19 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-08-11 23:28:19 +0100 |
| commit | bcd7a0c1c205833bf353fd2172c0fe8e656e6d83 (patch) | |
| tree | bf83e0dc50d33cd0ca5347eb27d3887818123977 /challenge-020 | |
| parent | aead9bcce41f3b264301fcbb95883f70917f552f (diff) | |
| parent | d60696fd5015f0de3f3d80f4f183ccfe681c4092 (diff) | |
| download | perlweeklychallenge-club-bcd7a0c1c205833bf353fd2172c0fe8e656e6d83.tar.gz perlweeklychallenge-club-bcd7a0c1c205833bf353fd2172c0fe8e656e6d83.tar.bz2 perlweeklychallenge-club-bcd7a0c1c205833bf353fd2172c0fe8e656e6d83.zip | |
Merge pull request #505 from rabbiveesh/master
Added answers to challenge 20. Many regexes
Diffstat (limited to 'challenge-020')
| -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 | 25 | ||||
| -rw-r--r-- | challenge-020/veesh-goldman/perl5/t/factors.t | 17 |
4 files changed, 46 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..31c6dedd42 --- /dev/null +++ b/challenge-020/veesh-goldman/perl5/ch-02.pl @@ -0,0 +1,25 @@ +#! /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; + my $pair = sum_of_proper_divisors($start); + #sometimes the sum of the divisors IS the number. But that's not amicable + return 0 if $pair == $start; + return $start == sum_of_proper_divisors $pair; +} + + +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; |
