aboutsummaryrefslogtreecommitdiff
path: root/challenge-175
diff options
context:
space:
mode:
Diffstat (limited to 'challenge-175')
-rw-r--r--challenge-175/ulrich-rieke/cpp/ch-2.cpp55
-rw-r--r--challenge-175/ulrich-rieke/haskell/ch-1.hs15
-rw-r--r--challenge-175/ulrich-rieke/haskell/ch-2.hs14
-rw-r--r--challenge-175/ulrich-rieke/perl/ch-1.pl22
-rw-r--r--challenge-175/ulrich-rieke/perl/ch-2.pl65
-rw-r--r--challenge-175/ulrich-rieke/raku/ch-1.raku19
-rw-r--r--challenge-175/ulrich-rieke/raku/ch-2.raku28
7 files changed, 218 insertions, 0 deletions
diff --git a/challenge-175/ulrich-rieke/cpp/ch-2.cpp b/challenge-175/ulrich-rieke/cpp/ch-2.cpp
new file mode 100644
index 0000000000..2f910a3bfc
--- /dev/null
+++ b/challenge-175/ulrich-rieke/cpp/ch-2.cpp
@@ -0,0 +1,55 @@
+#include <iostream>
+#include <vector>
+#include <numeric>
+#include <algorithm>
+
+int my_gcd( int a , int b ) { //Euclid's algorithm
+ if ( b >= a )
+ std::swap( a , b ) ;
+ while ( a != b ) {
+ a = a - b ;
+ if ( b >= a )
+ std::swap( a , b ) ;
+ }
+ return a ;
+}
+
+int myPhi( int n ) {
+ std::vector<int> totatives ;
+ for ( int i = 1 ; i < n ; i++ ) {
+ if ( my_gcd( i , n ) == 1 )
+ totatives.push_back( i ) ;
+ }
+ return totatives.size( ) ;
+}
+
+bool isPerfectTotient( int n ) {
+ if ( n == 1 ) {
+ return false ;
+ }
+ int current = n ;
+ std::vector<int> totients ;
+ do {
+ current = myPhi( current ) ;
+ totients.push_back( current ) ;
+ } while ( current != 1 ) ;
+ return std::accumulate( totients.begin( ) , totients.end( ) , 0 ) == n ;
+}
+
+int main( ) {
+ std::vector<int> perfectTotients ;
+ int current = 1 ;
+ while ( perfectTotients.size( ) != 20 ) {
+ if ( isPerfectTotient( current ) ) {
+ perfectTotients.push_back( current ) ;
+ }
+ current++ ;
+ }
+ for ( int i : perfectTotients ) {
+ std::cout << i ;
+ if ( i != perfectTotients.back( ) )
+ std::cout << ',' ;
+ }
+ std::cout << std::endl ;
+ return 0 ;
+}
diff --git a/challenge-175/ulrich-rieke/haskell/ch-1.hs b/challenge-175/ulrich-rieke/haskell/ch-1.hs
new file mode 100644
index 0000000000..5175584c08
--- /dev/null
+++ b/challenge-175/ulrich-rieke/haskell/ch-1.hs
@@ -0,0 +1,15 @@
+module Challenge175
+ where
+import Data.Time.Calendar
+
+findLastSunday :: Integer -> Int -> Day
+findLastSunday year month = myFunc $ fromGregorian year month
+(gregorianMonthLength year month )
+ where
+ myFunc :: Day -> Day
+ myFunc d = until ( (== Sunday ) . dayOfWeek ) change d
+ where
+ change = addDays (- 1 )
+
+solution :: Integer -> [String]
+solution year = map (\m -> showGregorian $ findLastSunday year m ) [1 .. 12]
diff --git a/challenge-175/ulrich-rieke/haskell/ch-2.hs b/challenge-175/ulrich-rieke/haskell/ch-2.hs
new file mode 100644
index 0000000000..998bd240a4
--- /dev/null
+++ b/challenge-175/ulrich-rieke/haskell/ch-2.hs
@@ -0,0 +1,14 @@
+module Challenge175_2
+ where
+
+chi :: Int -> Int
+chi n = length $ filter (\i -> gcd i n == 1 ) [1 .. n - 1]
+
+isPerfectTotient :: Int -> Bool
+isPerfectTotient n = (sum $ tail $ until ( (== 1 ) . last ) step [n]) == n
+where
+ step :: [Int] -> [Int]
+ step list = list ++ [ chi $ last list]
+
+solution :: [Int]
+solution = take 20 $ filter isPerfectTotient [1 , 2 ..]
diff --git a/challenge-175/ulrich-rieke/perl/ch-1.pl b/challenge-175/ulrich-rieke/perl/ch-1.pl
new file mode 100644
index 0000000000..408ab32dd9
--- /dev/null
+++ b/challenge-175/ulrich-rieke/perl/ch-1.pl
@@ -0,0 +1,22 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use DateTime ;
+
+my $year = $ARGV[0] ;
+my @lastDays = ( 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31) ;
+my $dt1 = DateTime->new( year => $year , month => 12 , day => 10 ) ;
+if ( $dt1->is_leap_year ) {
+ $lastDays[1] = 29 ;
+}
+my @lastSundays ;
+for my $month( 1 .. 12 ) {
+ my $dt = DateTime->new( year => $year , month => $month , day =>
+ $lastDays[ $month - 1 ] ) ;
+ while ( $dt->day_of_week != 7 ) {
+ $dt->subtract( days => 1 ) ;
+ }
+ push @lastSundays , $dt ;
+}
+map { say $_->ymd} @lastSundays ;
diff --git a/challenge-175/ulrich-rieke/perl/ch-2.pl b/challenge-175/ulrich-rieke/perl/ch-2.pl
new file mode 100644
index 0000000000..15924eaddb
--- /dev/null
+++ b/challenge-175/ulrich-rieke/perl/ch-2.pl
@@ -0,0 +1,65 @@
+#!/usr/bin/perl ;
+use strict ;
+use warnings ;
+use feature 'say' ;
+use List::Util qw ( reduce ) ;
+
+sub gcd { #Euclid's algorithm!
+ my $firstNum = shift ;
+ my $secondNum = shift ;
+ if ( $secondNum >= $firstNum ) {
+ my $swapped = $firstNum ;
+ $firstNum = $secondNum ;
+ $secondNum = $swapped ;
+ }
+ while ( $firstNum != $secondNum ) {
+ $firstNum = $firstNum - $secondNum ;
+ if ( $secondNum >= $firstNum ) {
+ my $swapped = $firstNum ;
+ $firstNum = $secondNum ;
+ $secondNum = $swapped ;
+ }
+ }
+ return $firstNum ;
+}
+
+sub myPhi {
+ my $number = shift ;
+ my @totatives ;
+ for my $i ( 1 .. $number - 1 ) {
+ if ( gcd( $i , $number ) == 1 ) {
+ push @totatives, $i ;
+ }
+ }
+ return scalar( @totatives ) ;
+}
+
+sub isPerfectTotient {
+ my $number = shift ;
+ if ( $number == 1 ) {
+ return 0 ;
+ }
+ my $start = $number ;
+ my @totients ;
+ do {
+ $start = myPhi( $start ) ;
+ push @totients, $start ;
+ } until ( $start == 1 ) ;
+ my $sum = reduce { $a + $b } @totients ;
+ if ( $sum == $number ) {
+ return 1 ;
+ }
+ else {
+ return 0 ;
+ }
+}
+
+my @perfectTotients ;
+my $current = 1 ;
+while ( scalar( @perfectTotients ) != 20 ) {
+ if ( isPerfectTotient( $current ) ) {
+ push @perfectTotients, $current ;
+ }
+ $current++ ;
+}
+say join( ',' , @perfectTotients ) ;
diff --git a/challenge-175/ulrich-rieke/raku/ch-1.raku b/challenge-175/ulrich-rieke/raku/ch-1.raku
new file mode 100644
index 0000000000..42c2278d4a
--- /dev/null
+++ b/challenge-175/ulrich-rieke/raku/ch-1.raku
@@ -0,0 +1,19 @@
+use v6 ;
+
+sub MAIN( Int $year ) {
+ my @lastDays = ( 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 ,
+ 31 ) ;
+ if ( Date.new("$year-01-01").is-leap-year) {
+ @lastDays[ 1 ] = 29 ;
+ }
+ my @lastSundays ;
+ for (1 .. 12 ) -> $month {
+ my $d = Date.new( year => $year , month => $month , day =>
+ @lastDays[ $month - 1 ] ) ;
+ while ( $d.day-of-week != 7 ) {
+ $d = $d.earlier( day => 1 ) ;
+ }
+ @lastSundays.push( $d ) ;
+ }
+ .say for @lastSundays.map( { .Str } ) ;
+}
diff --git a/challenge-175/ulrich-rieke/raku/ch-2.raku b/challenge-175/ulrich-rieke/raku/ch-2.raku
new file mode 100644
index 0000000000..390b1809b3
--- /dev/null
+++ b/challenge-175/ulrich-rieke/raku/ch-2.raku
@@ -0,0 +1,28 @@
+use v6 ;
+
+sub myPhi( Int $n --> Int ) {
+ return (1 .. $n - 1).grep( { $_ gcd $n == 1 }).elems ;
+}
+
+sub isPerfectTotient( Int $n is copy --> Bool ) {
+ if ( $n == 1 ) {
+ return False ;
+ }
+ my @totatives ;
+ my Int $current = $n ;
+ repeat {
+ $current = myPhi( $current ) ;
+ @totatives.push( $current ) ;
+ } until ( $current == 1 ) ;
+ return ([+] @totatives) == $n ;
+}
+
+my @perfectTotients ;
+my Int $current = 1 ;
+while ( @perfectTotients.elems != 20 ) {
+ if ( isPerfectTotient( $current ) ) {
+ @perfectTotients.push( $current ) ;
+ }
+ $current++ ;
+}
+say @perfectTotients ;