diff options
| author | E. Choroba <choroba@matfyz.cz> | 2021-06-08 00:06:34 +0200 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2021-06-08 00:06:34 +0200 |
| commit | 62e48cb5d256786931c8803194f13386732cb534 (patch) | |
| tree | a4bac3ef8d865d6ea54c9f15ee04a088c8d499af | |
| parent | 4830b1d50fd610eb2424fe284cb48a2795e0478c (diff) | |
| download | perlweeklychallenge-club-62e48cb5d256786931c8803194f13386732cb534.tar.gz perlweeklychallenge-club-62e48cb5d256786931c8803194f13386732cb534.tar.bz2 perlweeklychallenge-club-62e48cb5d256786931c8803194f13386732cb534.zip | |
Add solutions to 116: Number Sequence & Sum of Squares by E. Choroba
| -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(); |
