aboutsummaryrefslogtreecommitdiff
path: root/challenge-150
diff options
context:
space:
mode:
authorMohammad S Anwar <mohammad.anwar@yahoo.com>2022-02-02 17:40:31 +0000
committerMohammad S Anwar <mohammad.anwar@yahoo.com>2022-02-02 17:40:31 +0000
commit6fd6b4eaedb3b19ae8d913ba078c9d69f48227dd (patch)
treefcab8c2d1554b9bba3ed5c0a029e106e698dcbd6 /challenge-150
parentea530bc5cf89e42ab0175eec72b2ebccfa3fdad4 (diff)
downloadperlweeklychallenge-club-6fd6b4eaedb3b19ae8d913ba078c9d69f48227dd.tar.gz
perlweeklychallenge-club-6fd6b4eaedb3b19ae8d913ba078c9d69f48227dd.tar.bz2
perlweeklychallenge-club-6fd6b4eaedb3b19ae8d913ba078c9d69f48227dd.zip
- Added solutions by Ulrich Rieke.
Diffstat (limited to 'challenge-150')
-rw-r--r--challenge-150/ulrich-rieke/cpp/ch-1.cpp23
-rw-r--r--challenge-150/ulrich-rieke/cpp/ch-2.cpp76
-rw-r--r--challenge-150/ulrich-rieke/haskell/ch-1.hs7
-rw-r--r--challenge-150/ulrich-rieke/haskell/ch-2.hs30
-rw-r--r--challenge-150/ulrich-rieke/perl/ch-1.pl33
-rw-r--r--challenge-150/ulrich-rieke/perl/ch-2.pl74
-rw-r--r--challenge-150/ulrich-rieke/raku/ch-1.raku25
-rw-r--r--challenge-150/ulrich-rieke/raku/ch-2.raku42
8 files changed, 310 insertions, 0 deletions
diff --git a/challenge-150/ulrich-rieke/cpp/ch-1.cpp b/challenge-150/ulrich-rieke/cpp/ch-1.cpp
new file mode 100644
index 0000000000..e2cb519390
--- /dev/null
+++ b/challenge-150/ulrich-rieke/cpp/ch-1.cpp
@@ -0,0 +1,23 @@
+#include <iostream>
+#include <vector>
+#include <string>
+
+int main( ) {
+ std::string firstnum ;
+ std::string secondnum ;
+ std::cout << "Enter a number string consisting of digits only!\n" ;
+ std::cin >> firstnum ;
+ std::cout << "Enter a second number string consisting of digits only!\n" ;
+ std::cin >> secondnum ;
+ while ( secondnum.length( ) != firstnum.length( ) ) {
+ std::cout << "second number string should be as long as the first one!\n" ;
+ std::cin >> secondnum ;
+ }
+ std::vector<std::string> fibonacciWords { firstnum , secondnum } ;
+ while ( fibonacciWords.back( ).length( ) < 51 ) {
+ fibonacciWords.push_back( fibonacciWords[ fibonacciWords.size( ) - 2 ] +
+ fibonacciWords.back( ) ) ;
+ }
+ std::cout << fibonacciWords.back( ).substr( 50 , 1 ) << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-150/ulrich-rieke/cpp/ch-2.cpp b/challenge-150/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..db49e43808
--- /dev/null
+++ b/challenge-150/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,76 @@
+#include <iostream>
+#include <vector>
+#include <set>
+#include <cmath>
+
+bool isPrime( int i ) {
+ if ( i == 1 )
+ return false ;
+ else {
+ int stop = std::sqrt( static_cast<double>( i ) ) ;
+ for ( int d = 2 ; d <= stop ; d++ )
+ if ( i % d == 0 )
+ return false ;
+ return true ;
+ }
+}
+
+std::vector<int> findDivisors( int n ) {
+ std::vector<int> divisors ;
+ for ( int i = 1 ; i < n + 1 ; i++ ) {
+ if ( n % 1 == 0 )
+ divisors.push_back( i ) ;
+ }
+ return divisors ;
+}
+
+std::vector<int> primeFactorialize( int n ) {
+ std::vector<int> primeDecomposition ;
+ std::vector<int> divisors = findDivisors( n ) ;
+ std::vector<int> primeFactors ;
+ for ( int i : divisors ) {
+ if ( isPrime( i ) )
+ primeFactors.push_back( i ) ;
+ }
+ int num = 0 ;
+ while ( n != 1 ) {
+ int nextFactor = primeFactors[ num ] ;
+ while ( n % nextFactor == 0 ) {
+ primeDecomposition.push_back( nextFactor ) ;
+ n /= nextFactor ;
+ }
+ if ( num + 1 < primeFactors.size( ) )
+ num++ ;
+ }
+ return primeDecomposition ;
+}
+
+bool isSquareFree( int n ) {
+ if ( n == 1 ) {
+ return true ;
+ }
+ else {
+ std::vector<int> primeFactors = primeFactorialize( n ) ;
+ std::set<int> numset( primeFactors.begin( ) , primeFactors.end( ) ) ;
+ return primeFactors.size( ) == numset.size( ) ;
+ }
+}
+
+int main( ) {
+ std::vector<int> squareFrees ;
+ int current = 1 ;
+ while ( current < 501 ) {
+ if ( isSquareFree( current ) )
+ squareFrees.push_back( current ) ;
+ current++ ;
+ }
+ int count = 1 ;
+ for ( int i : squareFrees ) {
+ std::cout << i << " " ;
+ count++ ;
+ if ( count % 15 == 0 )//improves the output slightly
+ std::cout << std::endl ;
+ }
+ std::cout << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-150/ulrich-rieke/haskell/ch-1.hs b/challenge-150/ulrich-rieke/haskell/ch-1.hs
new file mode 100644
index 0000000000..184f92c5e5
--- /dev/null
+++ b/challenge-150/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,7 @@
+module Challenge150
+ where
+
+--no lengthy input validation, I assume the 2 strings to be of the right shape
+solution :: String -> String -> Char
+solution firstNum secondNum = head $ drop 50 $ snd $ until( (>= 51 ) . length . snd )
+(\p -> (snd p , fst p ++ snd p )) ( firstNum, secondNum )
diff --git a/challenge-150/ulrich-rieke/haskell/ch-2.hs b/challenge-150/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..dd02702728
--- /dev/null
+++ b/challenge-150/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,30 @@
+module Challenge150_2
+ where
+import Control.Monad.State.Lazy
+import Data.List ( nub )
+
+divisors :: Int -> [Int]
+divisors n = [p | p <- [2 .. n] , mod n p == 0]
+
+decompose :: State (Int , [Int]) [Int]
+decompose = do
+ (d , factors ) <- get
+ if d == 1
+ then return factors
+ else do
+ put ( div d ( head $ divisors d ) , factors ++ [ head $ divisors d])
+ decompose
+
+myFactors :: Int -> [Int]
+myFactors n = evalState decompose ( n , [] )
+
+isSquareFree :: Int -> Bool
+isSquareFree num
+ |num == 1 = True
+ |otherwise = length list == ( length $ nub list )
+ where
+ list :: [Int]
+ list = myFactors num
+
+solution :: [Int]
+solution = filter isSquareFree [1 .. 500]
diff --git a/challenge-150/ulrich-rieke/perl/ch-1.pl b/challenge-150/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..4681d638c6
--- /dev/null
+++ b/challenge-150/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+
+say "Enter a number string!" ;
+my $A = <STDIN> ;
+chomp $A ;
+while ( $A !~ /\A\d+\z/ ) {
+ say "the string should consist of digits only!" ;
+ $A = <STDIN> ;
+ chomp $A ;
+}
+say "Enter a second number string!" ;
+my $B = <STDIN> ;
+chomp $B ;
+while ( $B !~ /\A\d+\z/ ) {
+ say "the string should consist of digits only!" ;
+ $B = <STDIN> ;
+ chomp $B ;
+}
+while ( length $B != length $A || $B !~ /\A\d+\z/ ) {
+ say ("second number string should be " . length( $A ) .
+ " characters long!") ;
+ say "digits only!" ;
+ $B = <STDIN> ;
+ chomp $B ;
+}
+my @fibonacciWords = ($A , $B) ;
+while ( length($fibonacciWords[-1]) < 51 ) {
+ push @fibonacciWords , $fibonacciWords[-2] . $fibonacciWords[-1] ;
+}
+say substr( $fibonacciWords[-1] , 50 , 1 ) ;
diff --git a/challenge-150/ulrich-rieke/perl/ch-2.pl b/challenge-150/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..356330a4ae
--- /dev/null
+++ b/challenge-150/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,74 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use POSIX ;
+
+sub isPrime {
+ my $number = shift ;
+ if ( $number == 1 ) {
+ return 0 ;
+ }
+ elsif ( $number == 2 ) {
+ return 1 ;
+ }
+ else {
+ my $root = sqrt( $number ) ;
+ my $limit = floor( $root ) ;
+ for my $i (2 .. $limit) {
+ if ( $number % $i == 0 ) {
+ return 0 ;
+ }
+ }
+ return 1 ;
+ }
+}
+
+sub primeFactorialize {
+ my $number = shift ;
+ my @possibleFactors ;
+ my @factors ;
+ if ( $number == 1 ) {
+ push @factors , 1 ;
+ }
+ else {
+ if ( isPrime( $number ) ) {
+ push @factors, 1 , $number ;
+ }
+ else {
+ @possibleFactors = grep { isPrime( $_ ) } (2 ..
+ int( $number / 2 )) ; #pick all prime numbers from list of divisors
+ push @factors, 1 ;
+ while ( $number != 1 ) {
+ my $nextFactor = shift @possibleFactors ;
+ while ( $number % $nextFactor == 0 ) {#keep checking all prime factors
+ push @factors, $nextFactor ; #more than once
+ $number /= $nextFactor ;
+ }
+ }
+ }
+ }
+ return @factors ;
+}
+
+sub isSquareFree {
+ my $number = shift ;
+ my @primeFactors = primeFactorialize( $number ) ;
+ my %factors ;
+ for my $i ( @primeFactors ) {
+ $factors{ $i }++ ;
+ }
+#if it is square free the number of different prime factors should be equal to
+#the number of prime factors
+ return scalar( keys %factors ) == scalar( @primeFactors ) ;
+}
+
+my @square_frees ;
+my $current = 1 ;
+while ( $current < 501 ) {
+ if ( isSquareFree( $current ) ) {
+ push @square_frees, $current ;
+ }
+ $current++ ;
+}
+say join( ',' , @square_frees ) ;
diff --git a/challenge-150/ulrich-rieke/raku/ch-1.raku b/challenge-150/ulrich-rieke/raku/ch-1.raku
new file mode 100644
index 0000000000..58818e22a4
--- /dev/null
+++ b/challenge-150/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,25 @@
+use v6 ;
+
+say "Enter a first number string!" ;
+my $a = $*IN.get ;
+while ( $a !~~ /^\d+$/ ) {
+ say "The string should only consist of numbers!" ;
+ $a = $*IN.get ;
+}
+say "Enter a second number string!" ;
+my $b = $*IN.get ;
+while ( $b !~~ /^\d+$/ ) {
+ say "The string should only consist of numbers!" ;
+ $b = $*IN.get ;
+}
+while ( $b.chars != $a.chars || $b !~~ /^\d+$/ ) {
+ say "second number string should be {$a.chars} characters long!" ;
+ say "digits only!" ;
+ $b = $*IN.get ;
+}
+my @fibowords ;
+@fibowords.push( $a , $b ) ;
+while ( @fibowords[*-1].chars < 51 ) {
+ @fibowords.push( @fibowords[*-2] ~ @fibowords[*-1] ) ;
+}
+say @fibowords[*-1].substr( 50 , 1 ) ;
diff --git a/challenge-150/ulrich-rieke/raku/ch-2.raku b/challenge-150/ulrich-rieke/raku/ch-2.raku
new file mode 100644
index 0000000000..b58fa06bd0
--- /dev/null
+++ b/challenge-150/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,42 @@
+use v6 ;
+
+sub primeFactorization( Int $n is copy ) {
+ my @possibleFactors ;
+ my @factors ;
+ if ( $n == 1 ) {
+ @factors.push( 1 ) ;
+ }
+ else {
+ if ( $n.is-prime ) {
+ @factors.push( 1 , $n ) ;
+ }
+ else {
+ @possibleFactors = (2 .. $n div 2).grep( {.is-prime} ) ;
+ @factors.push( 1 ) ;
+ while ( $n != 1 ) {
+ my $nextFactor = @possibleFactors.shift ;
+ while ( $n %% $nextFactor ) {
+ @factors.push( $nextFactor ) ;
+ $n div= $nextFactor ;
+ }
+ }
+ }
+ }
+ return @factors ;
+}
+
+sub isSquareFree( Int $n is copy --> Bool ) {
+ my @primeFactors = primeFactorization( $n ) ;
+ my $primeSet = @primeFactors.Set ;
+ return $primeSet.elems == @primeFactors.elems ;
+}
+
+my @square-frees ;
+my $current = 1 ;
+while ( $current < 501 ) {
+ if ( isSquareFree( $current ) ) {
+ @square-frees.push( $current ) ;
+ }
+ $current++ ;
+}
+say join( ',' , @square-frees ) ;