aboutsummaryrefslogtreecommitdiff
path: root/challenge-252
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-01-16 18:28:40 +0000
committerGitHub <noreply@github.com>2024-01-16 18:28:40 +0000
commit0743c3a72e7b35b3c6a67b249b6c7d7e322cc0f7 (patch)
tree80ee2dc22ab89d3e25ea8b95edb847a5e9f924f2 /challenge-252
parentd72ff2c5235efaa859226b030c21de25028cbfb8 (diff)
parent9282ab98cbd52bace35f36e5cff3e6366a85b6fb (diff)
downloadperlweeklychallenge-club-0743c3a72e7b35b3c6a67b249b6c7d7e322cc0f7.tar.gz
perlweeklychallenge-club-0743c3a72e7b35b3c6a67b249b6c7d7e322cc0f7.tar.bz2
perlweeklychallenge-club-0743c3a72e7b35b3c6a67b249b6c7d7e322cc0f7.zip
Merge pull request #9406 from choroba/ech252
Solve 252: Special Numbers & Unique Sum Zero by E. Choroba
Diffstat (limited to 'challenge-252')
-rw-r--r--challenge-252/e-choroba/cpp/ch-1.cpp21
-rw-r--r--challenge-252/e-choroba/cpp/ch-2.cpp41
-rwxr-xr-xchallenge-252/e-choroba/perl/ch-1.pl16
-rwxr-xr-xchallenge-252/e-choroba/perl/ch-2.pl20
4 files changed, 98 insertions, 0 deletions
diff --git a/challenge-252/e-choroba/cpp/ch-1.cpp b/challenge-252/e-choroba/cpp/ch-1.cpp
new file mode 100644
index 0000000000..ec85e36bf0
--- /dev/null
+++ b/challenge-252/e-choroba/cpp/ch-1.cpp
@@ -0,0 +1,21 @@
+#include <cassert>
+#include <cmath>
+#include <iostream>
+#include <vector>
+
+long special_numbers(std::vector<long> ints) {
+ size_t s = ints.size();
+ long r = 0;
+ for (size_t i = 0; i <= s; ++i) {
+ if (s % (i + 1) == 0) {
+ r += std::pow(ints[i], 2);
+ }
+ }
+ return r;
+}
+
+int main(int argc, char* argv[]) {
+ assert(special_numbers({1, 2, 3, 4}) == 21);
+ assert(special_numbers({2, 7, 1, 19, 18, 3}) == 63);
+ std::cout << "Ok." << std::endl;
+}
diff --git a/challenge-252/e-choroba/cpp/ch-2.cpp b/challenge-252/e-choroba/cpp/ch-2.cpp
new file mode 100644
index 0000000000..e10d27aae6
--- /dev/null
+++ b/challenge-252/e-choroba/cpp/ch-2.cpp
@@ -0,0 +1,41 @@
+#include <cassert>
+#include <iostream>
+#include <vector>
+
+std::vector<int> unique_sum_zero(int n) {
+ std::vector<int> v;
+ if (n % 2) {
+ v = {0};
+ }
+ for (int i = 1; i <= n / 2; ++i) {
+ v.insert(v.end(), {i, -i});
+ }
+ return v;
+}
+
+int main(int argc, char* argv[]) {
+
+ auto u1 = unique_sum_zero(1);
+ assert(u1.size() == 1);
+ assert(u1[0] == 0);
+
+ auto u2 = unique_sum_zero(2);
+ assert(u2.size() == 2);
+ assert(u2[0] == 1);
+ assert(u2[1] == -1);
+
+ auto u3 = unique_sum_zero(3);
+ assert(u3.size() == 3);
+ assert(u3[0] == 0);
+ assert(u3[1] == 1);
+ assert(u3[2] == -1);
+
+ auto u4 = unique_sum_zero(4);
+ assert(u4.size() == 4);
+ assert(u4[0] == 1);
+ assert(u4[1] == -1);
+ assert(u4[2] == 2);
+ assert(u4[3] == -2);
+
+ std::cout << "Ok." << std::endl;
+}
diff --git a/challenge-252/e-choroba/perl/ch-1.pl b/challenge-252/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..6b5022d0c3
--- /dev/null
+++ b/challenge-252/e-choroba/perl/ch-1.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+use List::Util qw{ sum };
+
+sub special_numbers(@ints) {
+ my @s = @ints[grep 0 == @ints % ($_ + 1), 0 .. $#ints];
+ return sum(map $_ * $_, @s)
+}
+
+use Test::More tests => 2;
+
+is special_numbers(1, 2, 3, 4), 21, 'Example 1';
+is special_numbers(2, 7, 1, 19, 18, 3), 63, 'Example 2';
diff --git a/challenge-252/e-choroba/perl/ch-2.pl b/challenge-252/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..321c4500cc
--- /dev/null
+++ b/challenge-252/e-choroba/perl/ch-2.pl
@@ -0,0 +1,20 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use experimental qw( signatures );
+
+sub unique_sum_zero($n) {
+ return [(0) x ($n % 2), map { $_, -$_ } 1 .. $n / 2]
+}
+
+use Test::More tests => 7;
+use List::Util qw{ sum uniq };
+
+subtest "n=$_" => sub {
+ plan tests => 3;
+ my $output = unique_sum_zero($_);
+ is scalar @$output, $_, 'length';
+ my @u = uniq(@$output);
+ is scalar @u, $_, 'uniq';
+ is sum(@$output), 0, 'sum 0';
+} for 1 .. 7;