diff options
Diffstat (limited to 'challenge-144')
| -rw-r--r-- | challenge-144/ulrich-rieke/haskell/ch-1.hs | 18 | ||||
| -rw-r--r-- | challenge-144/ulrich-rieke/haskell/ch-2.hs | 19 | ||||
| -rw-r--r-- | challenge-144/ulrich-rieke/perl/ch-1.pl | 37 | ||||
| -rw-r--r-- | challenge-144/ulrich-rieke/perl/ch-2.pl | 58 | ||||
| -rw-r--r-- | challenge-144/ulrich-rieke/raku/ch-1.raku | 11 | ||||
| -rw-r--r-- | challenge-144/ulrich-rieke/raku/ch-2.raku | 37 |
6 files changed, 180 insertions, 0 deletions
diff --git a/challenge-144/ulrich-rieke/haskell/ch-1.hs b/challenge-144/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..cf9264c44a --- /dev/null +++ b/challenge-144/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,18 @@ +module Challenge144 + where +import Data.List ( subsequences , sort ) + +solution :: [Int] +solution = sort $ filter ( <= 100 ) ((map ( ^ 2 ) primes) ++ theList) + where + theList :: [Int] + theList = map product $ filter( (== 2) . length ) + $ subsequences primes + primes :: [Int] + primes = filter isPrime [2 .. 100] + +divisors :: Int -> [Int] +divisors n = filter (\i -> mod n i == 0 ) [1 .. n] + +isPrime :: Int -> Bool +isPrime n = divisors n == [1 , n] diff --git a/challenge-144/ulrich-rieke/haskell/ch-2.hs b/challenge-144/ulrich-rieke/haskell/ch-2.hs new file mode 100644 index 0000000000..92ee606c2f --- /dev/null +++ b/challenge-144/ulrich-rieke/haskell/ch-2.hs @@ -0,0 +1,19 @@ +module Challenge144_2 + where +import Data.List ( sort , subsequences ) + +count :: Eq a => [a] -> a -> Int +count list num = length $ filter ( (== num) ) list + +addUlam :: [Int] -> [Int] +addUlam list + |length list == 2 = list ++ [sum list] + |otherwise = list ++ [head $ filter (\s -> (count sums s == 1) && + s > last list) sums] + where + sums :: [Int] + sums = sort $ map sum $ filter ( (== 2 ) . length ) $ subsequences list + +solution :: Int -> Int -> [Int] +solution u v = head $ dropWhile( (< 10 ) . length ) $ iterate +addUlam [u , v] diff --git a/challenge-144/ulrich-rieke/perl/ch-1.pl b/challenge-144/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..7784e9cd2b --- /dev/null +++ b/challenge-144/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,37 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use Algorithm::Combinatorics qw ( combinations ) ; +use POSIX ; +use feature 'say' ; + +sub isPrime { + my $number = shift ; + if ( $number == 2 ) { + return 1 ; + } + else { + my $limit = floor(sqrt( $number )) + 1 ; + my $current = 2 ; + while ( $current < $limit ) { + if ( $number % $current == 0 ) { + return 0 ; + } + $current++ ; + } + } + return 1 ; +} + +my @primes = grep { isPrime( $_ )} (2 .. 100 ) ; +my @products ; +my $iter = combinations( \@primes , 2 ) ; +while ( my $c = $iter->next ) { + push @products , $c->[0] * $c->[1] ; +} +my @smallPrimes = grep { $_ < 12 } @primes ; +for my $num ( @smallPrimes ) { + push @products , $num * $num ; +} +my @sorted = sort { $a <=> $b } @products ; +say join( ',' , grep { $_ <= 100 } @sorted ) ; diff --git a/challenge-144/ulrich-rieke/perl/ch-2.pl b/challenge-144/ulrich-rieke/perl/ch-2.pl new file mode 100644 index 0000000000..6d1e8377fe --- /dev/null +++ b/challenge-144/ulrich-rieke/perl/ch-2.pl @@ -0,0 +1,58 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use Algorithm::Combinatorics qw ( combinations ) ; +use List::Util qw ( sum ) ; + +sub count { + my $array = shift ; + my $number = shift ; + my $counted = 0 ; + my @lookIn = @$array ; + for my $elem( @lookIn ) { + if ( $elem == $number ) { + $counted++ ; + } + } + return $counted ; +} + +my $u = $ARGV[ 0 ] ; +my $v = $ARGV[ 1 ] ; +while ( $u !~ /\A\d+\z/ || $v !~ /\A\d+\z/ ) { + say "Please enter 2 integers!" ; + $u = <STDIN> ; + chomp $u ; + $v = <STDIN> ; + chomp $v ; +} +my @ulams ; +push @ulams , $u , $v ; +@ulams = sort { $a <=> $b } @ulams ; +while ( scalar( @ulams ) < 10 ) { + if ( scalar( @ulams ) == 2 ) { + push @ulams, $ulams[0] + $ulams[ 1 ] ; + } + else { + my @sums ; + my $iter = combinations( \@ulams, 2 ) ; + while ( my $c = $iter->next ) { + push @sums , sum( @$c ) ; + } + my @sorted = sort { $a <=> $b } @sums ; + my $elements = scalar( @sorted ) ; + my $i = 0 ; + while ( $i < $elements ) { + if ( $sorted[ $i ] > $ulams[ -1 ] && count( \@sorted , + $sorted[ $i ] ) == 1 ) { + push @ulams, $sorted[ $i ] ; + last ; + } + else { + $i++ ; + } + } + } +} +say join( ',' , @ulams ) ; diff --git a/challenge-144/ulrich-rieke/raku/ch-1.raku b/challenge-144/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..50a1ae82a8 --- /dev/null +++ b/challenge-144/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,11 @@ +use v6 ; + +my @semiprimes ; +my @primes = (2 .. 100).grep( { .is-prime} ) ; +my @combis = @primes.combinations( 2 ).map( { $_[0] * $_[1] } ) ; +for @primes -> $prime { + @combis.push( $prime * $prime ) ; +} +@combis .= sort ; +my @solution = @combis.grep( { $_ <= 100 } ) ; +say @solution ; diff --git a/challenge-144/ulrich-rieke/raku/ch-2.raku b/challenge-144/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..2bf1be0a2a --- /dev/null +++ b/challenge-144/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,37 @@ +use v6 ; + +sub count( @array , Int $needle ) { + my $counted = 0 ; + for @array -> $number { + if ( $number == $needle ) { + $counted++ ; + } + } + return $counted ; +} + +sub MAIN( Int $u , Int $v ) { + my @ulams = ( $u , $v ).sort( { $^a <=> $^b } ) ; + while ( @ulams.elems < 10 ) { + if ( @ulams.elems == 2 ) { + @ulams.push( [+] @ulams ) ; + } + else { + my @combis = @ulams.combinations( 2 ) ; + my @sums = @combis.map( {$_[0] + $_[1] } ).sort ; + my $elements = @sums.elems ; + my $i = 0 ; + while ( $i < $elements ) { + if ( @sums[ $i ] > @ulams[ *-1 ] && count( @sums , @sums[ $i ] ) + == 1 ) { + @ulams.push( @sums[ $i ] ) ; + last ; + } + else { + $i++ ; + } + } + } + } + say @ulams ; +} |
