diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-30 00:04:58 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2023-03-30 00:04:58 +0100 |
| commit | f7e17efb23f4b03f6baef73bfb22521c06d6e47f (patch) | |
| tree | f6a00362aa034a5e73941d94016b3526b9a70057 /challenge-167 | |
| parent | 45a5b2b46fc08d6e7451b9b16f43118a752ecfb3 (diff) | |
| download | perlweeklychallenge-club-f7e17efb23f4b03f6baef73bfb22521c06d6e47f.tar.gz perlweeklychallenge-club-f7e17efb23f4b03f6baef73bfb22521c06d6e47f.tar.bz2 perlweeklychallenge-club-f7e17efb23f4b03f6baef73bfb22521c06d6e47f.zip | |
Add Perl solution
Diffstat (limited to 'challenge-167')
| -rw-r--r-- | challenge-167/paulo-custodio/perl/ch-1.pl | 76 | ||||
| -rw-r--r-- | challenge-167/paulo-custodio/perl/ch-2.pl | 68 | ||||
| -rw-r--r-- | challenge-167/paulo-custodio/t/test-1.yaml | 5 | ||||
| -rw-r--r-- | challenge-167/paulo-custodio/t/test-2.yaml | 15 |
4 files changed, 164 insertions, 0 deletions
diff --git a/challenge-167/paulo-custodio/perl/ch-1.pl b/challenge-167/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..92dff3782f --- /dev/null +++ b/challenge-167/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,76 @@ +#!/usr/bin/perl + +# Challenge 167 +# +# Task 1: Circular Prime +# Submitted by: Mohammad S Anwar +# +# Write a script to find out first 10 circular primes having at least 3 digits +# (base 10). +# +# Please checkout wikipedia for more information. +# +# A circular prime is a prime number with the property that the number +# generated at each intermediate step when cyclically permuting its (base 10) +# digits will also be prime. +# +# Output +# +# 113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939, 199933 + +use Modern::Perl; + +sub is_prime { + my($n) = @_; + return 0 if $n <= 1; + return 1 if $n <= 3; + return 0 if ($n % 2)==0 || ($n % 3)==0; + for (my $i = 5; $i*$i <= $n; $i += 6) { + return 0 if ($n % $i)==0 || ($n % ($i+2))==0; + } + return 1; +} + +sub next_prime { + my($n) = @_; + return 2 if $n <= 1; + my $p = $n; + 1 while !is_prime(++$p); + return $p; +} + +sub is_circular_prime { + my($n)=@_; + state %seen; + return 0 if $seen{$n}++; + return 0 if $n<10; + return 0 if $n =~ /[024568]/; + my @rotations; + for (1..length($n)) { + push @rotations,$n; + return 0 if !is_prime($n); + $n=substr($n,1).substr($n,0,1); + } + $seen{$_}++ for @rotations; + return 1; +} + +sub next_circular_prime { + my($n)=@_; + $n++; + while (!is_circular_prime($n)) { + $n=next_prime($n); + } + return $n; +} + +@ARGV or die "usage: ch-1.pl n\n"; +my $n=shift; +my @cprimes; +my $cp=1; +my %seen; +while (@cprimes<$n) { + $cp=next_circular_prime($cp); + push @cprimes,$cp if $cp>=100; +} +say join ", ", @cprimes; diff --git a/challenge-167/paulo-custodio/perl/ch-2.pl b/challenge-167/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..180fbfdc48 --- /dev/null +++ b/challenge-167/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,68 @@ +#!/usr/bin/perl + +# Challenge 167 +# +# Task 2: Gamma Function +# Submitted by: Mohammad S Anwar +# +# Implement subroutine gamma() using the Lanczos approximation method. +# +# [2022-05-31] +# +# Ryan Thompson wrote an interesting blog explaining the subject in details. +# Highly recommended if you are looking for more information. BLOG. +# +# Example +# +# print gamma(3); # 1.99 +# print gamma(5); # 24 +# print gamma(7); # 719.99 + +use Modern::Perl; +use Math::Complex qw( :pi Re Im ); +use List::Util 'sum'; + +# https://en.wikipedia.org/wiki/Lanczos_approximation + +use constant g => 7; +my @p = ( + 0.99999999999980993, + 676.5203681218851, + -1259.1392167224028, + 771.32342877765313, + -176.61502916214059, + 12.507343278686905, + -0.13857109526572012, + 9.9843695780195716e-6, + 1.5056327351493116e-7 +); + +use constant EPSILON => 1e-07; + +sub drop_imag { + my($z)=@_; + if (abs(Im($z))<=EPSILON) { + $z=Re($z); + } + return $z; +} + +sub gamma { + my($z)=@_; + my $y; + if (Re($z) < 0.5) { + $y = pi / (sin(pi * $z) * gamma(1 - $z)); # Reflection formula + } + else { + $z -= 1; + my $x = $p[0]; + for my $i (1..$#p) { + $x += $p[$i] / ($z + $i); + } + my $t = $z + g + 0.5; + $y = sqrt(2 * pi) * $t ** ($z + 0.5) * exp(-$t) * $x; + } + return drop_imag($y); +} + +say gamma(Math::Complex->new(shift)); diff --git a/challenge-167/paulo-custodio/t/test-1.yaml b/challenge-167/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..6955e2fe41 --- /dev/null +++ b/challenge-167/paulo-custodio/t/test-1.yaml @@ -0,0 +1,5 @@ +- setup: + cleanup: + args: 10 + input: + output: 113, 197, 199, 337, 1193, 3779, 11939, 19937, 193939, 199933 diff --git a/challenge-167/paulo-custodio/t/test-2.yaml b/challenge-167/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..e575042550 --- /dev/null +++ b/challenge-167/paulo-custodio/t/test-2.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 3 + input: + output: 2 +- setup: + cleanup: + args: 5 + input: + output: 24 +- setup: + cleanup: + args: 7 + input: + output: 720.000000000001 |
