diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-06-15 06:28:41 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-06-15 06:28:41 +0100 |
| commit | 640d609b76d6f98366ddb8edb35959cf3102e11a (patch) | |
| tree | e67c53a9d1f32277f9decaf92b4009bcbc0508c4 /challenge-169 | |
| parent | 4e67de07fef7fabe2a2ebd5d371d8644c83de1b9 (diff) | |
| parent | 5cf0768375d1e0df139eec109433726256ed489a (diff) | |
| download | perlweeklychallenge-club-640d609b76d6f98366ddb8edb35959cf3102e11a.tar.gz perlweeklychallenge-club-640d609b76d6f98366ddb8edb35959cf3102e11a.tar.bz2 perlweeklychallenge-club-640d609b76d6f98366ddb8edb35959cf3102e11a.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-169')
| -rwxr-xr-x | challenge-169/e-choroba/perl/ch-1.pl | 24 | ||||
| -rwxr-xr-x | challenge-169/e-choroba/perl/ch-2.pl | 25 | ||||
| -rw-r--r-- | challenge-169/ulrich-rieke/cpp/ch-1.cpp | 46 | ||||
| -rw-r--r-- | challenge-169/ulrich-rieke/cpp/ch-2.cpp | 66 | ||||
| -rw-r--r-- | challenge-169/ulrich-rieke/haskell/ch-1.hs | 26 | ||||
| -rw-r--r-- | challenge-169/ulrich-rieke/haskell/ch-2.hs | 25 | ||||
| -rw-r--r-- | challenge-169/ulrich-rieke/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-169/ulrich-rieke/perl/ch-2.pl | 67 | ||||
| -rw-r--r-- | challenge-169/ulrich-rieke/raku/ch-1.raku | 30 | ||||
| -rw-r--r-- | challenge-169/ulrich-rieke/raku/ch-2.raku | 33 |
10 files changed, 379 insertions, 0 deletions
diff --git a/challenge-169/e-choroba/perl/ch-1.pl b/challenge-169/e-choroba/perl/ch-1.pl new file mode 100755 index 0000000000..ef863b1cbc --- /dev/null +++ b/challenge-169/e-choroba/perl/ch-1.pl @@ -0,0 +1,24 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Math::Prime::Util qw{ factor }; + +sub brilliant_numbers { + my ($tally) = @_; + my @brilliant_numbers; + my $n = 1; + while (@brilliant_numbers < $tally) { + my @f = factor(++$n); + push @brilliant_numbers, $n + if 2 == @f + && length($f[0]) == length($f[1]); + } + return \@brilliant_numbers +} + +use Test::More tests => 1; + +is_deeply brilliant_numbers(20), + [4, 6, 9, 10, 14, 15, 21, 25, 35, 49, 121, + 143, 169, 187, 209, 221, 247, 253, 289, 299]; diff --git a/challenge-169/e-choroba/perl/ch-2.pl b/challenge-169/e-choroba/perl/ch-2.pl new file mode 100755 index 0000000000..48e9f7c4eb --- /dev/null +++ b/challenge-169/e-choroba/perl/ch-2.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl +use warnings; +use strict; + +use Math::Prime::Util qw{ factor gcd }; + +sub achilles_numbers { + my ($tally) = @_; + my $n = 1; + my @achilles_numbers; + while (@achilles_numbers < $tally) { + my %factors; + ++$factors{$_} for factor(++$n); + next if grep $_ == 1, values %factors; # Powerful. + + push @achilles_numbers, $n + if 1 == gcd(values %factors); # Not perfect. + } + return \@achilles_numbers +} + +use Test::More tests => 1; +is_deeply achilles_numbers(20), + [72, 108, 200, 288, 392, 432, 500, 648, 675, 800, 864, + 968, 972, 1125, 1152, 1323, 1352, 1372, 1568, 1800]; diff --git a/challenge-169/ulrich-rieke/cpp/ch-1.cpp b/challenge-169/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..2d4d8011f4 --- /dev/null +++ b/challenge-169/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,46 @@ +#include <iostream> +#include <vector> +#include <string> +#include <algorithm> + +std::vector<int> primeDecompose( int n ) { + std::vector<int> primeFactors ; + int current = 2 ; + while ( n != 1 ) { + if ( n % current == 0 ) { + primeFactors.push_back( current ) ; + n /= current ; + } + else { + current++ ; + } + } + return primeFactors ; +} + +bool isBrilliant( int n ) { + std::vector<int> primeFactors( primeDecompose( n ) ) ; + if ( primeFactors.size( ) == 2 ) { + std::vector<std::string> primeNumbers( 2 ) ; + std::transform( primeFactors.begin( ) , primeFactors.end( ) , + primeNumbers.begin( ) , []( int i ){ return std::to_string( i ) ;} ) ; + return primeNumbers.begin()->length( ) == primeNumbers.back().length( ) ; + } + else { + return false ; + } +} + +int main( ) { + std::vector<int> brilliantNumbers ; + int current = 2 ; + while ( brilliantNumbers.size( ) != 20 ) { + if ( isBrilliant( current ) ) + brilliantNumbers.push_back( current ) ; + current++ ; + } + for ( int i : brilliantNumbers ) + std::cout << i << " " ; + std::cout << std::endl ; + return 0 ; +} diff --git a/challenge-169/ulrich-rieke/cpp/ch-2.cpp b/challenge-169/ulrich-rieke/cpp/ch-2.cpp new file mode 100644 index 0000000000..ffbcfcdd34 --- /dev/null +++ b/challenge-169/ulrich-rieke/cpp/ch-2.cpp @@ -0,0 +1,66 @@ +#include <iostream> +#include <algorithm> +#include <map> +#include <vector> +#include <numeric> +#include <iterator> + +std::vector<int> primeDecompose( int n ) { + std::vector<int> primeFactors ; + int current = 2 ; + while ( n != 1 ) { + if ( n % current == 0 ) { + primeFactors.push_back( current ) ; + n /= current ; + } + else { + current++ ; + } + } + return primeFactors ; +} + +int my_gcd( int a , int b ) { + std::vector<int> firstFactors( primeDecompose( a ) ) ; + std::vector<int> secondFactors( primeDecompose( b ) ) ; + std::vector<int> common ; + std::set_intersection( firstFactors.begin( ) , firstFactors.end( ) , + secondFactors.begin( ) , secondFactors.end( ) , + std::inserter( common, common.begin( ) )) ; + return std::accumulate( common.begin( ) , common.end( ) , 1 , + std::multiplies( ) ) ; +} + +bool isAchilles( int n ) { + std::vector<int> primeFactors( primeDecompose( n ) ) ; + std::map<int , int> factorCount ; + for ( int i : primeFactors ) { + factorCount[ i ]++ ; + } + std::vector<int> exponents ; + for ( auto it = factorCount.begin( ) ; it != factorCount.end( ) ; ++it ) { + exponents.push_back( it->second ) ; + } + if ( *std::min_element(exponents.begin( ) , exponents.end( ) ) >= 2 ) { + int start = *exponents.begin( ) ; + return (std::accumulate( exponents.begin( ) , exponents.end( ) , start , + []( int a , int b ){ return my_gcd( a , b ) ; } ) == 1 ) ; + } + else + return false ; +} + +int main( ) { + std::vector<int> achillesNumbers ; + int current = 2 ; + while ( achillesNumbers.size( ) != 20 ) { + if ( isAchilles( current ) ) { + achillesNumbers.push_back( current ) ; + } + current++ ; + } + for ( int i : achillesNumbers ) + std::cout << i << " " ; + std::cout << std::endl ; + return 0 ; +} diff --git a/challenge-169/ulrich-rieke/haskell/ch-1.hs b/challenge-169/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..704ef0c9b1 --- /dev/null +++ b/challenge-169/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,26 @@ +module Challenge169 + where + +divisors :: Int -> [Int] +divisors n = [d | d <- [2 .. n] , mod n d == 0 ] + +primeDecompose :: Int -> [Int] +primeDecompose n = snd $ until ( (== 1) . fst ) step (n , [] ) +where + step :: ( Int , [Int] ) -> ( Int , [Int] ) + step ( d , list ) = ( div d h , list ++ [h] ) + where + h = head $ divisors d + +isBrilliant :: Int -> Bool +isBrilliant n = (( length primeFactors ) == 2) && (l1 == l2 ) +where + primeFactors :: [Int] + primeFactors = primeDecompose n + l1 :: Int + l1 = length $ show $ head primeFactors + l2 :: Int + l2 = length $ show $ last primeFactors + +solution :: [Int] +solution = take 20 $ filter isBrilliant [2 , 3 ..] diff --git a/challenge-169/ulrich-rieke/haskell/ch-2.hs b/challenge-169/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..f3d2184277 --- /dev/null +++ b/challenge-169/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,25 @@ +module Challenge169_2 + where +import Data.List ( group ) + +divisors :: Int -> [Int] +divisors n = [d | d <- [2 .. n] , mod n d == 0 ] + +primeDecompose :: Int -> [Int] +primeDecompose n = snd $ until ( (== 1) . fst ) step (n , [] ) +where + step :: ( Int , [Int] ) -> ( Int , [Int] ) + step ( d , list ) = ( div d h , list ++ [h] ) + where + h = head $ divisors d + +isAchilles :: Int -> Bool +isAchilles n = myCondition && ( (foldl1 gcd theLengths ) == 1 ) +where + theLengths :: [Int] + theLengths = map length $ group $ primeDecompose n + myCondition :: Bool + myCondition = if null theLengths then False else (minimum theLengths) >= 2 + +solution :: [Int] +solution = take 20 $ filter isAchilles [2 , 3 ..] diff --git a/challenge-169/ulrich-rieke/perl/ch-1.pl b/challenge-169/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..59be1f066b --- /dev/null +++ b/challenge-169/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; + +sub primeDecompose { + my $number = shift ; + my $current = 2 ; + my @primeFactors ; + while ( $number != 1 ) { + if ( $number % $current == 0 ) { + push @primeFactors, $current ; + $number /= $current ; + } + else { + $current++ ; + } + } + return @primeFactors ; +} + +sub isBrilliant { + my $number = shift ; + my @primeFactors = primeDecompose( $number ) ; + return ( (scalar( @primeFactors ) == 2 ) && ( length( $primeFactors[0] ) + == length( $primeFactors[1] ))) ; +} + +my @brilliantNumbers ; +my $current = 1 ; +while ( scalar( @brilliantNumbers ) != 20 ) { + if ( isBrilliant( $current ) ) { + push @brilliantNumbers , $current ; + } + $current++ ; +} +say join( ',' , @brilliantNumbers ) ; diff --git a/challenge-169/ulrich-rieke/perl/ch-2.pl b/challenge-169/ulrich-rieke/perl/ch-2.pl new file mode 100644 index 0000000000..d2f5c0af99 --- /dev/null +++ b/challenge-169/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,67 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw ( min max reduce ) ; + +sub primeDecompose { + my $number = shift ; + my $current = 2 ; + my @primeFactors ; + while ( $number != 1 ) { + if ( $number % $current == 0 ) { + push @primeFactors, $current ; + $number /= $current ; + } + else { + $current++ ; + } + } + return @primeFactors ; +} + +sub gcd { + my $first = shift ; + my $second = shift ; + my %firstDivisors ; + my %secondDivisors ; + for my $n ( 1 .. $first ) { + if ( $first % $n == 0 ) { + $firstDivisors{ $n }++ ; + } + } + for my $n ( 1 .. $second ) { + if ( $second % $n == 0 ) { + $secondDivisors{ $n }++ ; + } + } + my @common = grep { exists ($secondDivisors{$_}) } keys %firstDivisors ; + return max( @common ) ; +} + +sub isAchilles { + my $number = shift ; + my @primeFactors = primeDecompose( $number ) ; + my %achilles ; + for my $n ( @primeFactors ) { + $achilles{$n}++ ; + } + my @frequencies = values( %achilles ) ; + my $minFactor = min( @frequencies ) ; + if ( $minFactor < 2 ) { + return 0 ; + } + else { + return ( (reduce { gcd( $a , $b ) } @frequencies) == 1 ) ; + } +} + +my @achillesNumbers ; +my $current = 2 ; +while ( scalar( @achillesNumbers ) != 20 ) { + if ( isAchilles( $current ) ) { + push @achillesNumbers , $current ; + } + $current++ ; +} +say join( ',' , @achillesNumbers ) ; diff --git a/challenge-169/ulrich-rieke/raku/ch-1.raku b/challenge-169/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..ce2705bccc --- /dev/null +++ b/challenge-169/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,30 @@ +use v6 ; + +sub primeDecompose( Int $n is copy ) { + my @primeFactors ; + my $current = 2 ; + while ( $n != 1 ) { + while ( not ( $n %% $current ) ) { + $current++ ; + } + @primeFactors.push( $current ) ; + $n div= $current ; + } + return @primeFactors ; +} + +sub isBrilliant( Int $n is copy --> Bool ) { + my @primeFactors = primeDecompose( $n ) ; + return ( @primeFactors.elems == 2 ) && ( ~(@primeFactors[0]).chars == + ~(@primeFactors[1].chars )) ; +} + +my @brilliantNumbers ; +my $current = 2 ; +while ( @brilliantNumbers.elems != 20 ) { + if ( isBrilliant( $current ) ) { + @brilliantNumbers.push( $current ) ; + } + $current++ ; +} +say @brilliantNumbers.join( ',') ; diff --git a/challenge-169/ulrich-rieke/raku/ch-2.raku b/challenge-169/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..f04f39e218 --- /dev/null +++ b/challenge-169/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,33 @@ +use v6 ; + +sub primeDecompose( Int $n is copy ) { + my @primeFactors ; + my $current = 2 ; + while ( $n != 1 ) { + while ( not ( $n %% $current ) ) { + $current++ ; + } + @primeFactors.push( $current ) ; + $n div= $current ; + } + return @primeFactors ; +} + +sub isAchillesNumber( Int $n is copy --> Bool ) { + my @primeFactors = primeDecompose( $n ) ; + my %achilles ; + for @primeFactors -> $i { + %achilles{~$i}++ ; + } + return (%achilles.values.min >= 2) && (([gcd] %achilles.values) == 1 ) ; +} + +my @achillesNumbers ; +my $current = 2 ; +while (@achillesNumbers.elems != 20 ) { + if ( isAchillesNumber( $current ) ) { + @achillesNumbers.push( $current ) ; + } + $current++ ; +} +say @achillesNumbers.join( ', ' ) ; |
