diff options
| -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; |
