diff options
| -rw-r--r-- | challenge-109/wanderdoc/perl/ch-1.pl | 52 | ||||
| -rw-r--r-- | challenge-109/wanderdoc/perl/ch-2.pl | 55 |
2 files changed, 107 insertions, 0 deletions
diff --git a/challenge-109/wanderdoc/perl/ch-1.pl b/challenge-109/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..d7895f9566 --- /dev/null +++ b/challenge-109/wanderdoc/perl/ch-1.pl @@ -0,0 +1,52 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +Write a script to generate first 20 Chowla Numbers, named after, Sarvadaman D. S. Chowla, a London born Indian American mathematician. It is defined as: C(n) = (sum of divisors of n) - 1 - n +Output: 0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21 +=cut + + + + + + +use Test::More; + +sub chowla_num +{ + my $num = $_[0]; + my $div_sum = div_sum($num); + my $chowla_num = $num == 1 ? ($div_sum - 1) : ($div_sum - 1 - $num); + return $chowla_num; +} + + + +sub div_sum +{ + my $num = $_[0]; + my $sum = 0; + return $num if $num < 2; + for my $i ( 1 .. $num ) + { + $sum += $i if ( $num % $i == 0 ) + } + return $sum; +} + + + +my @output; + + + +for my $n ( 1 .. 78 ) +{ + push @output, chowla_num($n); +} + +is_deeply([@output], [0,0,0,2,0,5,0,6,3,7,0,15,0,9,8,14,0,20,0,21,10,13,0,35,5,15,12,27,0,41,0,30,14,19,12,54,0,21,16, + 49,0,53,0,39,32,25,0,75,7,42,20,45,0,65,16,63,22,31,0,107,0,33,40,62,18,77,0,57,26,73,0,122,0,39,48,63,18,89], 'A048050'); +done_testing();
\ No newline at end of file diff --git a/challenge-109/wanderdoc/perl/ch-2.pl b/challenge-109/wanderdoc/perl/ch-2.pl new file mode 100644 index 0000000000..fbe323e597 --- /dev/null +++ b/challenge-109/wanderdoc/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given four squares as below with numbers named a,b,c,d,e,f,g. +Write a script to place the given unique numbers in the square box so that sum of numbers in each box is the same. +Box 1: a + b +Box 2: b + c + d +Box 3: d + e + f +Box 4: f + g +=cut + + + + + + +use Algorithm::Combinatorics qw (permutations); +use List::Util qw(sum); +use Scalar::Util qw(looks_like_number); + +my @data = (-2, -1, 0, 1, 8, 9, 10); # 1 .. 7; + + + +my $iter = permutations(\@data); +while (my $p = $iter->next) +{ + my $test = test_sums(@$p); + print join(":\t", $test, join('',('|', join('|', + join(" ", @$p[0 .. 1]), join(" ", @$p[1 .. 3]), + join(" ", @$p[3 .. 5]), join(" ", @$p[5 .. 6])), '|')) ), $/, $/ + if looks_like_number($test); +} + + + + + +sub test_sums +{ + my @arr = @_; + + my $box1 = sum(@arr[0 .. 1]); + my $box2 = sum(@arr[1 .. 3]); + my $box3 = sum(@arr[3 .. 5]); + my $box4 = sum(@arr[5 .. 6]); + + if ( $box1 == $box2 and $box1 == $box3 and $box1 == $box4 ) + { + return $box1; + } + return 'Nope'; +}
\ No newline at end of file |
