diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-06-08 10:51:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-08 10:51:50 +0100 |
| commit | dbb2a26e571bc283c778f5c6f5ae82e8be89650c (patch) | |
| tree | d03c7009135a987e73c50609f2289c0db3c4a554 /challenge-116 | |
| parent | d39aff77f1d5c74a64aa1e10a2a6513e79322d10 (diff) | |
| parent | 62e48cb5d256786931c8803194f13386732cb534 (diff) | |
| download | perlweeklychallenge-club-dbb2a26e571bc283c778f5c6f5ae82e8be89650c.tar.gz perlweeklychallenge-club-dbb2a26e571bc283c778f5c6f5ae82e8be89650c.tar.bz2 perlweeklychallenge-club-dbb2a26e571bc283c778f5c6f5ae82e8be89650c.zip | |
Merge pull request #4229 from choroba/ech116
Add solutions to 116: Number Sequence & Sum of Squares by E. Choroba
Diffstat (limited to 'challenge-116')
| -rwxr-xr-x | challenge-116/e-choroba/perl/ch-1.pl | 26 | ||||
| -rwxr-xr-x | challenge-116/e-choroba/perl/ch-2.pl | 21 |
2 files changed, 47 insertions, 0 deletions
diff --git a/challenge-116/e-choroba/perl/ch-1.pl b/challenge-116/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..1e80688d70 --- /dev/null +++ b/challenge-116/e-choroba/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/perl +use warnings; +use strict; + +sub number_sequence { + my ($n) = @_; + my $ln = length $n; + for my $length (1 .. $ln / 2) { + my $start = substr $n, 0, $length; + my $i = my $n2 = $start; + $n2 .= ++$i while length $n2 < $ln; + return [$start .. $i] if $n2 == $n; + } + return [$n] +} + +use Test::More; + +is_deeply number_sequence(1234), [1, 2, 3, 4], 'Example 1'; +is_deeply number_sequence(91011), [9, 10, 11], 'Example 2'; +is_deeply number_sequence(10203), [10203], 'Example 3'; + +is_deeply number_sequence(417418419420), [417, 418, 419, 420], 'Longer'; +is_deeply number_sequence(80910), [80910], 'Leading zero'; + +done_testing(); diff --git a/challenge-116/e-choroba/perl/ch-2.pl b/challenge-116/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..ef8509ec98 --- /dev/null +++ b/challenge-116/e-choroba/perl/ch-2.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use List::Util qw{ sum }; + +sub sum_of_squares { + my ($n) = @_; + my $s = sqrt sum(map $_ * $_, split //, $n); + return $s == int $s ? 1 : 0 +} + +use Test::More; + +is sum_of_squares(34), 1, 'Example 1'; +is sum_of_squares(50), 1, 'Example 2'; +is sum_of_squares(52), 0, 'Example 3'; + +is sum_of_squares(1234567890123456966), 1, 'Larger (23*23)'; + +done_testing(); |
