diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-07-25 19:57:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-07-25 19:57:25 +0100 |
| commit | 054d42c9bf1cd0bf3472094e21045abf22c4c354 (patch) | |
| tree | e30650cbbb00aec20cd7d7fa3c9c18a8b34b2866 /challenge-175 | |
| parent | 1bdc238658f52c494040ccae602d5ecf75cb9bec (diff) | |
| parent | b79c6b4d33d5bcf9289e499255d85ed92a0cb993 (diff) | |
| download | perlweeklychallenge-club-054d42c9bf1cd0bf3472094e21045abf22c4c354.tar.gz perlweeklychallenge-club-054d42c9bf1cd0bf3472094e21045abf22c4c354.tar.bz2 perlweeklychallenge-club-054d42c9bf1cd0bf3472094e21045abf22c4c354.zip | |
Merge pull request #6506 from kjetillll/challenge-175-kjetillll
https://theweeklychallenge.org/blog/perl-weekly-challenge-175/
Diffstat (limited to 'challenge-175')
| -rw-r--r-- | challenge-175/kjetillll/perl/ch-1.pl | 10 | ||||
| -rw-r--r-- | challenge-175/kjetillll/perl/ch-2.pl | 27 |
2 files changed, 37 insertions, 0 deletions
diff --git a/challenge-175/kjetillll/perl/ch-1.pl b/challenge-175/kjetillll/perl/ch-1.pl new file mode 100644 index 0000000000..5137aca00d --- /dev/null +++ b/challenge-175/kjetillll/perl/ch-1.pl @@ -0,0 +1,10 @@ +#!/usr/bin/perl +use Time::Local; +my $y = shift || 1900+(localtime)[5]; +for my $m (1..12){ + for(reverse 1..31){ + is_sunday($y,$m,$_) and printf("%04d-%02d-%02d\n", $y, $m, $_) and last + } +} + +sub is_sunday{ my($y,$m,$d)=@_; (eval{ timegm(0,0,0,$d,$m-1,$y) } / 86400 - 3) % 7 == 0 } diff --git a/challenge-175/kjetillll/perl/ch-2.pl b/challenge-175/kjetillll/perl/ch-2.pl new file mode 100644 index 0000000000..5dc7946afe --- /dev/null +++ b/challenge-175/kjetillll/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl +# prints the first 20 Perfect Totient Numbers: +# 3 9 15 27 39 81 111 183 243 255 327 363 471 729 2187 +# https://theweeklychallenge.org/blog/perl-weekly-challenge-175/ +# https://en.wikipedia.org/wiki/Perfect_totient_number +# use Math::Prime::Util::GMP 'gcd'; # ...is faster, but not a core module +# use Math::Prime::Util::GMP 'totient'; # ...is screamingly fast! + +use strict; use warnings; +use Memoize; +memoize('totient'); #good idea, unless using GMP +#memoize('gcd'); #bad idea, runs slower + +sub totient { my $n=shift; 0+grep gcd($_,$n)==1, 1..$n-1 } + +sub gcd { my($a,$b)=@_;($a,$b)=($b,$a)if$a>$b;($a,$b)=($b%$a,$a)while$a;$b } + +my $want = shift() // 20; +my $try = 1; +while( $want > 0 ){ + my $sum = 0; + my $n = $try; + $sum += $n = totient($n) while $n>1; + print "$try\n" and $want-- if $sum == $try; + $try+=2; +} + |
