From 04368ad24f28250401393f1096c9f6a323e9ef7a Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 15 Jan 2024 12:35:05 +0100 Subject: Solve 252: Special Numbers & Unique Sum Zero by E. Choroba --- challenge-252/e-choroba/perl/ch-1.pl | 16 ++++++++++++++++ challenge-252/e-choroba/perl/ch-2.pl | 20 ++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100755 challenge-252/e-choroba/perl/ch-1.pl create mode 100755 challenge-252/e-choroba/perl/ch-2.pl 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; -- cgit From 9282ab98cbd52bace35f36e5cff3e6366a85b6fb Mon Sep 17 00:00:00 2001 From: "E. Choroba" Date: Mon, 15 Jan 2024 18:32:09 +0100 Subject: Add C++ solutions --- challenge-252/e-choroba/cpp/ch-1.cpp | 21 ++++++++++++++++++ challenge-252/e-choroba/cpp/ch-2.cpp | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 challenge-252/e-choroba/cpp/ch-1.cpp create mode 100644 challenge-252/e-choroba/cpp/ch-2.cpp 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 +#include +#include +#include + +long special_numbers(std::vector 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 +#include +#include + +std::vector unique_sum_zero(int n) { + std::vector 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; +} -- cgit