diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-01-20 11:06:42 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-20 11:06:42 +0000 |
| commit | ac8392afeb871c0b388d45d2cbcf5fbfa9340fb2 (patch) | |
| tree | 2dea836ed640b0985e3858318d99dc64f3ea18ae | |
| parent | 9ea38fd04a87628ebec1a794428609f41db8b8b2 (diff) | |
| parent | 79d6fbc93380860e76f7f5c717d1c43bca6f4bbb (diff) | |
| download | perlweeklychallenge-club-ac8392afeb871c0b388d45d2cbcf5fbfa9340fb2.tar.gz perlweeklychallenge-club-ac8392afeb871c0b388d45d2cbcf5fbfa9340fb2.tar.bz2 perlweeklychallenge-club-ac8392afeb871c0b388d45d2cbcf5fbfa9340fb2.zip | |
Merge pull request #9426 from bn-ssotka/ch-252
challenge 252
| -rwxr-xr-x | challenge-252/bn-ssotka/raku/ch-01.raku | 33 | ||||
| -rwxr-xr-x | challenge-252/bn-ssotka/raku/ch-02.raku | 20 |
2 files changed, 53 insertions, 0 deletions
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 |
