From ef5caf159a754b6539cfcb5c9037669f1c43ecf5 Mon Sep 17 00:00:00 2001 From: Solathian Date: Sun, 5 Nov 2023 20:51:27 +0100 Subject: Added files for 241! --- challenge-241/solathian/perl/ch-1.pl | 30 ++++++++++++++++++++ challenge-241/solathian/perl/ch-2.pl | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 challenge-241/solathian/perl/ch-1.pl create mode 100644 challenge-241/solathian/perl/ch-2.pl diff --git a/challenge-241/solathian/perl/ch-1.pl b/challenge-241/solathian/perl/ch-1.pl new file mode 100644 index 0000000000..2d002c083a --- /dev/null +++ b/challenge-241/solathian/perl/ch-1.pl @@ -0,0 +1,30 @@ +#!usr/bin/perl +use v5.38; + +# Challenge 241 - 1 - Arithmetic Triplets + +say art(3, 0, 1, 4, 6, 7, 10); +say art(2, 4, 5, 6, 7, 8, 9); + +sub art($diff, @nums) +{ + my $number; + + for(my $i = 0; $i < @nums - 2; $i++ ) + { + for(my $j = $i; $j < @nums - 1; $j++ ) + { + for(my $k = $j; $k < @nums; $k++ ) + { + + if( (($nums[$j] - $nums[$i]) == $diff ) && (($nums[$k] - $nums[$j]) == $diff )) + { + # say join(",", $nums[$i], $nums[$j], $nums[$k]); + $number++; + } + } + } + } + + return $number; +} \ No newline at end of file diff --git a/challenge-241/solathian/perl/ch-2.pl b/challenge-241/solathian/perl/ch-2.pl new file mode 100644 index 0000000000..ac6c151973 --- /dev/null +++ b/challenge-241/solathian/perl/ch-2.pl @@ -0,0 +1,55 @@ +#!usr/bin/perl +use v5.38; + +# Challenge 241 - 2 - Prime Order + + +say primerOrder(11, 8, 27, 4); # 11, 4, 8, 27 + + +sub createPrimeArray($number) +{ + my @primes; + my $divisor = 2; + + + die"Number should be greater than 1" if($number < 2); + + while($number != 1) + { + unless ($number % $divisor ) + { + $number /= $divisor; + push(@primes, $divisor); + $divisor = 2; + } + else + { + $divisor++; + } + } + + return scalar @primes; +} + +sub primerOrder(@list) +{ + my %hash; # since the list is unique we can use a hash + my @result; + + foreach my $number (@list) + { + $hash{$number} = createPrimeArray($number); + } + + # sort by the number of primes, if equal, sort by the number + foreach my $key (sort {$hash{$a} <=> $hash{$b} || $a <=> $b } keys %hash) + { + push(@result, $key); + } + + # cause why not + $" = ', '; + "@result"; + +} \ No newline at end of file -- cgit