aboutsummaryrefslogtreecommitdiff
path: root/challenge-169
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-169')
-rw-r--r--challenge-169/ulrich-rieke/cpp/ch-1.cpp46
-rw-r--r--challenge-169/ulrich-rieke/cpp/ch-2.cpp66
-rw-r--r--challenge-169/ulrich-rieke/haskell/ch-1.hs26
-rw-r--r--challenge-169/ulrich-rieke/haskell/ch-2.hs25
-rw-r--r--challenge-169/ulrich-rieke/perl/ch-1.pl37
-rw-r--r--challenge-169/ulrich-rieke/perl/ch-2.pl67
-rw-r--r--challenge-169/ulrich-rieke/raku/ch-1.raku30
-rw-r--r--challenge-169/ulrich-rieke/raku/ch-2.raku33
8 files changed, 330 insertions, 0 deletions
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( ', ' ) ;