diff options
| author | Joelle Maslak <jmaslak@antelope.net> | 2019-08-11 11:44:02 -0600 |
|---|---|---|
| committer | Joelle Maslak <jmaslak@antelope.net> | 2019-08-11 11:44:02 -0600 |
| commit | 5e28e69f671565d2d0580c016e4260eb39fcc8e8 (patch) | |
| tree | c5bbe182ed19fbcf64dd35531f24ca91fe5a30ab /challenge-020 | |
| parent | a502f7b3d4ec12bf59139b3d8082bcc20ce03ce9 (diff) | |
| download | perlweeklychallenge-club-5e28e69f671565d2d0580c016e4260eb39fcc8e8.tar.gz perlweeklychallenge-club-5e28e69f671565d2d0580c016e4260eb39fcc8e8.tar.bz2 perlweeklychallenge-club-5e28e69f671565d2d0580c016e4260eb39fcc8e8.zip | |
Solution for 20.2 in P6 and P5
Diffstat (limited to 'challenge-020')
| -rwxr-xr-x | challenge-020/joelle-maslak/perl5/ch-2.pl | 49 | ||||
| -rwxr-xr-x | challenge-020/joelle-maslak/perl6/ch-2.p6 | 26 |
2 files changed, 75 insertions, 0 deletions
diff --git a/challenge-020/joelle-maslak/perl5/ch-2.pl b/challenge-020/joelle-maslak/perl5/ch-2.pl new file mode 100755 index 0000000000..dc240d181a --- /dev/null +++ b/challenge-020/joelle-maslak/perl5/ch-2.pl @@ -0,0 +1,49 @@ +#!/usr/bin/env perl + +use strict; +use warnings; + +use v5.22; + +# Turn on method signatures +use feature 'signatures'; +no warnings 'experimental::signatures'; + +use List::Util qw(max sum); +use integer; + +MAIN: { + my @factors; + my @results; + + my $i = 2; + while ( $i++ ) { + my $sum = factorsum($i); + $factors[$i] = $sum; + + if ( ( $sum > 1 ) && ( $sum < $i ) ) { + if ( $i == $factors[$sum] ) { + say "$sum $i"; + exit; + } + } + } + + say sum @results; +} + +sub factorsum($num) { + my @f; + my $sqrt = sqrt $num; + + push @f, 1; + if ( $sqrt * $sqrt == $num ) { push @f, $sqrt; } + + for ( my $i = 2; $i < $sqrt; $i++ ) { + if ( $num % $i != 0 ) { next; } + push @f, $i, $num / $i; + } + + return sum @f; +} + diff --git a/challenge-020/joelle-maslak/perl6/ch-2.p6 b/challenge-020/joelle-maslak/perl6/ch-2.p6 new file mode 100755 index 0000000000..6a2c4f8970 --- /dev/null +++ b/challenge-020/joelle-maslak/perl6/ch-2.p6 @@ -0,0 +1,26 @@ +#!/usr/bin/env perl6 +use v6; +use experimental :cached; + +# Smallest pair is a confusing term. Is it (A, B) where a is the +# smallest A, or is it where A+B is the smallest A+B? I'm going with +# the smallest A. + +MAIN: { + my $amicable = + grep { factorsum($^a) != $^a }, + grep { $^a == factorsum(factorsum($^a)) }, + 2..∞; + + say $amicable[0] ~ " " ~ factorsum($amicable[0]);; +} + +sub factorsum($num) is cached { + return sum + unique :with(&[==]), + grep { $^a != $num}, + map { |($^a, $num / $^a) }, + grep { $num %% $^a }, + 1..( Int(sqrt($num)) ); +} + |
