diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-04-22 16:30:57 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2021-04-22 16:30:57 +0100 |
| commit | 9571f967ddd4d11a0195c2b9f8f3fbb63fad9a15 (patch) | |
| tree | 6fca3d737ef81c2c9607aae90a42f415d24805ba /challenge-109 | |
| parent | ad34927f4671ddb3cf0c84392cc05d7c409c2eb4 (diff) | |
| download | perlweeklychallenge-club-9571f967ddd4d11a0195c2b9f8f3fbb63fad9a15.tar.gz perlweeklychallenge-club-9571f967ddd4d11a0195c2b9f8f3fbb63fad9a15.tar.bz2 perlweeklychallenge-club-9571f967ddd4d11a0195c2b9f8f3fbb63fad9a15.zip | |
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-109')
| -rw-r--r-- | challenge-109/ulrich-rieke/cpp/ch-1.cpp | 33 | ||||
| -rw-r--r-- | challenge-109/ulrich-rieke/haskell/ch-1.hs | 10 | ||||
| -rw-r--r-- | challenge-109/ulrich-rieke/perl/ch-1.pl | 34 | ||||
| -rw-r--r-- | challenge-109/ulrich-rieke/raku/ch-1.raku | 30 | ||||
| -rw-r--r-- | challenge-109/ulrich-rieke/raku/ch-2.raku | 114 |
5 files changed, 221 insertions, 0 deletions
diff --git a/challenge-109/ulrich-rieke/cpp/ch-1.cpp b/challenge-109/ulrich-rieke/cpp/ch-1.cpp new file mode 100644 index 0000000000..55a7a755a3 --- /dev/null +++ b/challenge-109/ulrich-rieke/cpp/ch-1.cpp @@ -0,0 +1,33 @@ +#include <iostream> +#include <vector> +#include <algorithm> +#include <iterator> +#include <numeric> + +std::vector<int> findDivisors( int n ) { + std::vector<int> divisors ; + if ( n > 3 ) { + for ( int i = 2 ; i < n ; i++ ) { + if ( n % i == 0 ) { + divisors.push_back( i ) ; + } + } + } + return divisors ; +} + +int main( ) { + std::vector<int> chowlanumbers ; + int n = 0 ; + while ( chowlanumbers.size( ) < 20 ) { + std::vector<int> divs = findDivisors( ++n ) ; + if ( divs.size( ) == 0 ) + chowlanumbers.push_back( 0 ) ; + else + chowlanumbers.push_back( std::accumulate( divs.begin( ) , divs.end( ) , 0 )) ; + } + std::copy( chowlanumbers.begin( ) , chowlanumbers.end( ) , + std::ostream_iterator<int>( std::cout , " " ) ) ; + std::cout << std::endl ; + return 0 ; +} diff --git a/challenge-109/ulrich-rieke/haskell/ch-1.hs b/challenge-109/ulrich-rieke/haskell/ch-1.hs new file mode 100644 index 0000000000..3c9c4b110c --- /dev/null +++ b/challenge-109/ulrich-rieke/haskell/ch-1.hs @@ -0,0 +1,10 @@ +module Challenge109 + where + +chowladivisors :: Int -> [Int] +chowladivisors n + |n `elem` [1 , 2 , 3] = [] + |otherwise = [d | d <- [2 .. n - 1] , mod n d == 0 ] + +solution :: [Int] +solution = take 20 $ map ( sum . chowladivisors) [1,2..] diff --git a/challenge-109/ulrich-rieke/perl/ch-1.pl b/challenge-109/ulrich-rieke/perl/ch-1.pl new file mode 100644 index 0000000000..a1772fa0b3 --- /dev/null +++ b/challenge-109/ulrich-rieke/perl/ch-1.pl @@ -0,0 +1,34 @@ +#!/usr/bin/perl ; +use strict ; +use warnings ; +use feature 'say' ; +use List::Util qw ( sum ) ; + +sub chowladivisors { + my $n = shift ; + if ( $n == 1 or $n == 2 or $n == 3 ) { + return ( ) ; + } + else { + my @divisors ; + for my $i (2 .. $n - 1) { + if ( $n % $i == 0 ) { + push @divisors , $i ; + } + } + return @divisors ; + } +} + +my @chowladivs = ( ) ; +my $n = 0 ; +while ( scalar @chowladivs < 20 ) { + my @divs = chowladivisors( ++$n ) ; + if ( scalar @divs == 0 ) { + push @chowladivs, 0 ; + } + else { + push @chowladivs , sum @divs ; + } +} +say join( ", " , @chowladivs ) ; diff --git a/challenge-109/ulrich-rieke/raku/ch-1.raku b/challenge-109/ulrich-rieke/raku/ch-1.raku new file mode 100644 index 0000000000..5d67b2fc41 --- /dev/null +++ b/challenge-109/ulrich-rieke/raku/ch-1.raku @@ -0,0 +1,30 @@ +use v6 ; + +sub chowladivisors( Int $n ) { + if ( $n (elem) Set( 1 , 2 , 3 ) ) { + return ( ) ; + } + else { + my @divisors = ( ) ; + for (2 .. $n - 1 ) -> $i { + if ( $n %% $i ) { + @divisors.push( $i ) ; + } + } + return @divisors ; + } +} + +my @chowlanumbers = ( ) ; +my $n = 0 ; +repeat { + $n++ ; + my @div = chowladivisors( $n ) ; + if ( @div ) { + @chowlanumbers.push( @div.sum ) ; + } + else { + @chowlanumbers.push( 0 ) ; + } +} until ( @chowlanumbers.elems == 20 ) ; +say @chowlanumbers ; diff --git a/challenge-109/ulrich-rieke/raku/ch-2.raku b/challenge-109/ulrich-rieke/raku/ch-2.raku new file mode 100644 index 0000000000..eff2027ee7 --- /dev/null +++ b/challenge-109/ulrich-rieke/raku/ch-2.raku @@ -0,0 +1,114 @@ +use v6 ; + +sub isValid( $theCombination ) { + if (( $theCombination[0].elems == 2 && $theCombination[1].elems == 3 + && $theCombination[2].elems == 3 && $theCombination[3].elems == 2 ) + && (($theCombination[0] (&) $theCombination[1]).elems == 1 && + ($theCombination[1] (&) $theCombination[2]).elems == 1 && + ($theCombination[2] (&) $theCombination[3]).elems == 1 )) { + my @intersections = ( ($theCombination[0] (&) $theCombination[1]) , + ($theCombination[1] (&) $theCombination[2]) , + ($theCombination[2] (&) $theCombination[3])) ; +#the intersections should be different from each other + return @intersections.unique.elems == 3 ; + } + else { + return False ; + } +} + +#find those combinations of 4 that are possible solutions, that is +#one combination of 2 at the beginning and the end and 2 combinations +#of 3 in the middle +sub is_possibleCombination( $aCombination ) { + if ( $aCombination[0].elems == 2 && $aCombination[1].elems == 3 + && $aCombination[2].elems == 3 && $aCombination[3].elems == 2 ) { + return True ; + } + return False ; +} + +my $numbers = (1 .. 7 ) ; +#we are interested in all those combinations of 2 and 3 numbers that +#share the same sum +my $commonSums = $numbers.combinations(2).map( {.sum} ) (&) + $numbers.combinations(3).map( {.sum } ) ; +my @two_elements = $numbers.combinations( 2 ).Array; +my @three_elements = $numbers.combinations( 3 ).Array ; +my @solutions ; #holds one or more valid solutions +#sum by sum , we check whether there are at least 2 different combinations +#of 2 or 3 numbers that add up to that sum. +for $commonSums.keys -> $sum { + my $combis_with_same_sum = Set.new( @two_elements.grep( + {.sum == $sum })) (|) + Set.new( @three_elements.grep( {.sum == $sum })) ; + if ( $combis_with_same_sum.keys.elems > 0 ) { +#if we have combinations of 2 and 3 numbers with the same sum we create +#combinations of 4, pick those with 2 pairs and 2 combinations of 3 and +#permutate them to see if there is a valid solution + my @possibleCombis ; + if ( $combis_with_same_sum.keys.grep( { .elems == 3 } ).elems >= 2 && + $combis_with_same_sum.keys.grep( { .elems == 2 } ).elems >= 2 ) { + @possibleCombis.push( $combis_with_same_sum.keys ) ; + } + for @possibleCombis -> $solution_candidate { + if ( $solution_candidate.elems > 4 ) { + my $fourCombis = $solution_candidate.combinations( 4 ) ; + for $fourCombis <-> $element { + for $element.Array <-> $subelement { + if ( is_possibleCombination( $subelement )) { + my $permus = $subelement.permutations ; + my @permulist = $permus.Array ; + for @permulist -> $permu { + if ( isValid( $permu ) ) { + @solutions.push( $permu ) ; + } + } + } + } + } + } + if ( $solution_candidate.elems == 4 ) { + if ( $solution_candidate.grep( {.elems == 2} ).elems == 2 + && $solution_candidate.grep( {.elems == 3} ).elems == 2 ) { + my $permus = $solution_candidate.permutations() ; + for $permus -> $permu { + if ( is_possibleCombination( $permu ) ) { + if ( isValid( $permu ) ) { + @solutions.push( $permu ) ; + } + } + } + } + } + } + } +} +#we have to "unwrap" the set intersection values by applying the .keys operator +#to the sets +for @solutions -> $quadruple { + my $sum = $quadruple[0].sum ; + my $b = $quadruple[0] (&) $quadruple[ 1 ] ; + $b = $b.keys[0] ; #"unwrap" the value and reassign! + my $a = $sum - $b ; + my $d = $quadruple[1] (&) $quadruple[2] ; + $d = $d.keys[ 0 ] ; + my $c = $sum - ($b + $d ) ; + my $f = $quadruple[2] (&) $quadruple[3] ; + $f = $f.keys[0] ; + my $e = $sum - ( $d + $f ) ; + my $g = $sum - $f ; + say "a = $a" ; + say "b = $b" ; + say "c = $c" ; + say "d = $d" ; + say "e = $e" ; + say "f = $f" ; + say "g = $g" ; + print "\n\n" ; + say "Box 1: a + b = $a + $b = {$a + $b}" ; + say "Box 2: b + c + d = $b + $c + $d = {$b + $c + $d}" ; + say "Box 3: d + e + f = $d + $e + $f = {$d + $e + $f}" ; + say "Box 4: f + g = $f + $g = {$f + $g}" ; + say "---------------------------------------------------------------------------" ; +} |
