diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-07-30 04:22:18 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-30 04:22:18 +0100 |
| commit | 6877679978a3d00925ac3a3be48f9733b15e2c8d (patch) | |
| tree | e8f8f2866bdf22b650bd16b7d57b6974f48bd01c | |
| parent | 1afe12860d33f4c4b736d7b7aaaf2c2de4cedb3a (diff) | |
| parent | c579eaf2faec2bcc6e7e2b5956ef512c39e0a245 (diff) | |
| download | perlweeklychallenge-club-6877679978a3d00925ac3a3be48f9733b15e2c8d.tar.gz perlweeklychallenge-club-6877679978a3d00925ac3a3be48f9733b15e2c8d.tar.bz2 perlweeklychallenge-club-6877679978a3d00925ac3a3be48f9733b15e2c8d.zip | |
Merge pull request #6522 from E7-87-83/newt
Week 175
| -rw-r--r-- | challenge-175/cheok-yin-fung/java/LastSunday.java | 53 | ||||
| -rw-r--r-- | challenge-175/cheok-yin-fung/julia/ch-1.jl | 21 | ||||
| -rw-r--r-- | challenge-175/cheok-yin-fung/perl/ch-1.pl | 52 | ||||
| -rw-r--r-- | challenge-175/cheok-yin-fung/perl/ch-2.pl | 19 | ||||
| -rw-r--r-- | challenge-175/cheok-yin-fung/raku/ch-1.raku | 46 | ||||
| -rw-r--r-- | challenge-175/cheok-yin-fung/raku/ch-2.raku | 39 |
6 files changed, 230 insertions, 0 deletions
diff --git a/challenge-175/cheok-yin-fung/java/LastSunday.java b/challenge-175/cheok-yin-fung/java/LastSunday.java new file mode 100644 index 0000000000..50b6432412 --- /dev/null +++ b/challenge-175/cheok-yin-fung/java/LastSunday.java @@ -0,0 +1,53 @@ +// The Weekly Challenge 175 +// Task 1 Last Sunday + +import java.time.LocalDate; + +public class LastSunday +{ + + public static void main(String[] args) + { + int year = Integer.parseInt(args[0]); + for (String date_str : last_sun_year(year)) + { + System.out.println(date_str); + } + } + + public static LocalDate last_sun_month (int yr, int month) + { + LocalDate temp = LocalDate.of(yr,month+1,1); + return temp.minusDays(gd(temp)); + } + + public static String[] last_sun_year(int yr) + { + String[] ans = new String[12]; + for (int i = 1; i<=11; i++) + { + ans[i-1] = last_sun_month(yr,i).toString(); + } + LocalDate temp = LocalDate.of(yr+1,1,1); + ans[11] = temp.minusDays(gd(temp)).toString(); + return ans; + } + + public static int gd(LocalDate date) + { + var a = date.getDayOfWeek(); + switch(a) + { + case MONDAY: return 1; + case TUESDAY: return 2; + case WEDNESDAY: return 3; + case THURSDAY: return 4; + case FRIDAY: return 5; + case SATURDAY: return 6; + case SUNDAY: return 7; + } + return 0; + } + + +} diff --git a/challenge-175/cheok-yin-fung/julia/ch-1.jl b/challenge-175/cheok-yin-fung/julia/ch-1.jl new file mode 100644 index 0000000000..68af0c76ce --- /dev/null +++ b/challenge-175/cheok-yin-fung/julia/ch-1.jl @@ -0,0 +1,21 @@ +# The Weekly Challenge - 137 +# Task 1 Long Year + +using Dates + +function last_sun_month(yr, month) + temp = Date(yr, month, 1) + return temp-Dates.Day(Dates.dayofweek(temp)) +end + +function last_sun_year(yr) + ans = [] + for i = 2:12 + push!(ans,last_sun_month(yr,i)) + end + ny = Date(yr+1, 1, 1) + push!(ans, ny-Dates.Day(Dates.dayofweek(ny))) + return ans +end + +last_sun_year(2022) diff --git a/challenge-175/cheok-yin-fung/perl/ch-1.pl b/challenge-175/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..ad5bdbb525 --- /dev/null +++ b/challenge-175/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,52 @@ +# The Weekly Challenge 175 +# Task 1 Last Sunday +use v5.30.0; +use Time::Local qw'timegm_nocheck'; +use Time::gmtime; + + +say join "\n", last_sun_year($ARGV[0] || 2022)->@*; + + +sub last_sun_month { + my ($m, $y)= @_; + my $n = gmtime timegm_nocheck 0, 0, 0, 1, $m+1, $y; + my $constant = $n->wday() == 0 ? 7 : $n->wday(); + return (gmtime timegm_nocheck 0, 0, 0, 1-$constant, $m+1, $y); +} + + + +sub date_str { + my $d_s = $_[0]; + return + ($d_s->year()+1900)."-" + .($d_s->mon()<=8 ? 0 : "").($d_s->mon()+1)."-" + .($d_s->mday()<10 ? 0 : "").($d_s->mday()) +} + + + +sub last_sun_year { + my $y = $_[0]-1900; + my @suns = map {last_sun_month($_, $y)} (0..10); + return [map {date_str $_} @suns, last_sun_month(-1, $y+1)]; +} + + +use Test::More tests => 1; +use Test::Deep; +cmp_deeply + last_sun_year(2022), + ["2022-01-30", + "2022-02-27", + "2022-03-27", + "2022-04-24", + "2022-05-29", + "2022-06-26", + "2022-07-31", + "2022-08-28", + "2022-09-25", + "2022-10-30", + "2022-11-27", + "2022-12-25"]; diff --git a/challenge-175/cheok-yin-fung/perl/ch-2.pl b/challenge-175/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..8096c138bf --- /dev/null +++ b/challenge-175/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,19 @@ +# The Weekly Challenge 175 +# Task 2 Perfect Totient Number +use v5.20.0; +use Math::Prime::Util qw/euler_phi/; + +my $p = 0; +for (my $n0 = 3; $n0 < 1_000_000_000; $n0 += 2) { + my $n = $n0; + my $sum = 0; + do { + $n = euler_phi($n); + $sum += $n; + } until ($sum > $n0 || $n == 1); + if ($sum == $n0) { + say $n0; + $p++; + } + last if $p >= ($ARGV[0] || 20); +} diff --git a/challenge-175/cheok-yin-fung/raku/ch-1.raku b/challenge-175/cheok-yin-fung/raku/ch-1.raku new file mode 100644 index 0000000000..946cf0a043 --- /dev/null +++ b/challenge-175/cheok-yin-fung/raku/ch-1.raku @@ -0,0 +1,46 @@ +# The Weekly Challenge 175 +# Task 1 Last Sunday +use v6; +use Test; + + +sub lastsun ($s) { + return $s.earlier(days => $s.day-of-week == 0 ?? 7 !! $s.day-of-week ); +} + + + +sub last_sun_year ($year) { + my @ans; + for (2..12) { + my $m = $_ < 10 ?? '0'~"$_" !! $_; + my $s = Date.new("2022-$m-01"); + my $n = lastsun $s; + @ans.push($n); + } + my $ny = Date.new("{$year+1}-01-01"); + my $n = lastsun $ny; + @ans.push($n); + return @ans; +} + + + +is( last_sun_year(2022), + ["2022-01-30", + "2022-02-27", + "2022-03-27", + "2022-04-24", + "2022-05-29", + "2022-06-26", + "2022-07-31", + "2022-08-28", + "2022-09-25", + "2022-10-30", + "2022-11-27", + "2022-12-25"] +); + +plan 1; + +done-testing; diff --git a/challenge-175/cheok-yin-fung/raku/ch-2.raku b/challenge-175/cheok-yin-fung/raku/ch-2.raku new file mode 100644 index 0000000000..50ff1cd650 --- /dev/null +++ b/challenge-175/cheok-yin-fung/raku/ch-2.raku @@ -0,0 +1,39 @@ +# The Weekly Challenge 175 +# Task 2 Perfect Totient Number +# (benchmark: real 0m36.228s user 0m34.853s sys 0m1.584s) +use v6; +use Prime::Factor; + + +sub totient ($n) { + my $x = 1; + my @s = prime-factors($n).unique; + for (2..$n-1) { + $x++; + for (@s) -> $i { + if ($_ % $i == 0) { + $x--; + last; + } + } + } + return $x; +} + + + +my $p = 0; +loop (my $n0 = 3; $n0 < 1_000_000_000; $n0 += 2 ) { + my $n = $n0; + my $sum = 0; + repeat { + $n = totient($n); + $sum += $n; + } until ($sum > $n0 || $n == 1); + if ($sum == $n0) { + say $n0; + $p++; + } + last if $p >= 20; +} + |
