From 79d6fbc93380860e76f7f5c717d1c43bca6f4bbb Mon Sep 17 00:00:00 2001 From: Scott Sotka Date: Fri, 19 Jan 2024 16:15:52 -0800 Subject: challenge 252 --- challenge-252/bn-ssotka/raku/ch-01.raku | 33 +++++++++++++++++++++++++++++++++ challenge-252/bn-ssotka/raku/ch-02.raku | 20 ++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100755 challenge-252/bn-ssotka/raku/ch-01.raku create mode 100755 challenge-252/bn-ssotka/raku/ch-02.raku diff --git a/challenge-252/bn-ssotka/raku/ch-01.raku b/challenge-252/bn-ssotka/raku/ch-01.raku new file mode 100755 index 0000000000..f222fd520e --- /dev/null +++ b/challenge-252/bn-ssotka/raku/ch-01.raku @@ -0,0 +1,33 @@ +#!/usr/bin/env raku + +# Write a script to find the sum of the squares of all special elements of the given array. +# An element $int[i] of @ints is called special if i divides n, i.e. n % i == 0. +# Where n is the length of the given array. Also the array is 1-indexed for the task + +sub find-squared-specials (@ints) { + my $n = @ints.elems; + my $sum = 0; + for @ints.keys -> $i { + $sum += @ints[$i] ** 2 if $n %% ($i + 1); + } + return $sum; +} +multi MAIN (*@ints) { + my $sum = find-squared-specials(@ints); + say $sum; +} + +multi MAIN () { + my @tests = ( + (1, 3, 5, 7, 9, 11, 13, 15), + (1, 2, 3, 4), + (2, 7, 1, 19, 18, 3) + ); + + for @tests -> @ints { + say @ints; + my $sum = find-squared-specials(@ints); + say "Sum:",$sum; + } + +} \ No newline at end of file diff --git a/challenge-252/bn-ssotka/raku/ch-02.raku b/challenge-252/bn-ssotka/raku/ch-02.raku new file mode 100755 index 0000000000..97117699fc --- /dev/null +++ b/challenge-252/bn-ssotka/raku/ch-02.raku @@ -0,0 +1,20 @@ +#!/usr/bin/env raku + +#You are given an integer, $n. +# +#Write a script to find an array containing $n unique integers such that they add up to zero. + +multi MAIN (Int $n where * > 0) { + my @array; + + my $n-items = ($n * 2) + 1; # we'll make sure we can't run out of numbers + while @array.unique.elems < $n { + # make an array of length $n - 1 containing unique random integers + @array = (^($n - 1)).map({ (^$n-items).pick - ($n-items/2).Int }); # we'll make sure we can't run out of numbers + # add up the array and negate the sum to get the last number + push @array, -[+] @array; + } + + # print the array + say @array; +} \ No newline at end of file -- cgit