From 6098b55bf66ef842c1afe1eeb68a46f85f34cc0c Mon Sep 17 00:00:00 2001 From: Jörg Sommrey <28217714+jo-37@users.noreply.github.com> Date: Mon, 20 Nov 2023 19:34:25 +0100 Subject: Solution to task 2 --- challenge-244/jo-37/perl/ch-2.pl | 65 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 challenge-244/jo-37/perl/ch-2.pl diff --git a/challenge-244/jo-37/perl/ch-2.pl b/challenge-244/jo-37/perl/ch-2.pl new file mode 100755 index 0000000000..4a51d8fba7 --- /dev/null +++ b/challenge-244/jo-37/perl/ch-2.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl -s + +use v5.16; +use Test2::V0; +use bigint; + +our ($tests, $examples); + +run_tests() if $tests || $examples; # does not return + +die < $b} @_; + my $power; + while (defined (my $min = $s[0])) { + while (my ($offs, $max) = each @s) { + $power += $min * $max**2 * ($offs ? 2**($offs - 1) : 1); + } + } continue { + shift @s; + } + + $power; +} + +### Examples and tests + +sub run_tests { + SKIP: { + skip "examples" unless $examples; + + is total_power(2, 1, 4), 141, 'example 1'; + } + + SKIP: { + + is total_power(2, 3, 5, 7), 1627, 'example from blog'; + is total_power((2) x 64), (2**64 - 1) * 2 * 2**2, + '147573952589676412920'; + } + + done_testing; + exit; +} -- cgit