aboutsummaryrefslogtreecommitdiff
path: root/challenge-167
diff options
context:
space:
mode:
authorAdam Russell <ac.russell@live.com>2022-06-05 09:45:26 -0400
committerAdam Russell <ac.russell@live.com>2022-06-05 09:45:26 -0400
commit8d088c47f0e6d7cab02f35914a822b1e7f1877ab (patch)
tree3bd5f28194d653ce7f1515203b4402fa284b5238 /challenge-167
parentb5d63d656dd32a537197b2164d10a8f56cbfc1ac (diff)
downloadperlweeklychallenge-club-8d088c47f0e6d7cab02f35914a822b1e7f1877ab.tar.gz
perlweeklychallenge-club-8d088c47f0e6d7cab02f35914a822b1e7f1877ab.tar.bz2
perlweeklychallenge-club-8d088c47f0e6d7cab02f35914a822b1e7f1877ab.zip
initial commit
Diffstat (limited to 'challenge-167')
-rw-r--r--challenge-167/adam-russell/blog.txt1
-rw-r--r--challenge-167/adam-russell/perl/ch-1.pl42
-rw-r--r--challenge-167/adam-russell/perl/ch-2.pl40
3 files changed, 83 insertions, 0 deletions
diff --git a/challenge-167/adam-russell/blog.txt b/challenge-167/adam-russell/blog.txt
new file mode 100644
index 0000000000..6c8de92de3
--- /dev/null
+++ b/challenge-167/adam-russell/blog.txt
@@ -0,0 +1 @@
+http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/06/05
diff --git a/challenge-167/adam-russell/perl/ch-1.pl b/challenge-167/adam-russell/perl/ch-1.pl
new file mode 100644
index 0000000000..7dda485fdc
--- /dev/null
+++ b/challenge-167/adam-russell/perl/ch-1.pl
@@ -0,0 +1,42 @@
+use strict;
+use warnings;
+##
+# Write a script to find out first 10 circular primes having at least 3 digits (base 10).
+##
+use boolean;
+use Math::Primality qw/is_prime/;
+
+sub is_circular_prime{
+ my($x) = @_;
+ my @digits = split(//, $x);
+ for my $i (0 .. @digits - 1){
+ @digits = (@digits[1 .. @digits - 1], $digits[0]);
+ my $candidate = join("", @digits) + 0;
+ return false if !is_prime($candidate);
+ }
+ return true;
+}
+
+sub first_n_circular_primes{
+ my($n) = @_;
+ my $i = 100;
+ my %circular;
+ my @circular_primes;
+ {
+ if(is_circular_prime($i) && !$circular{join("", sort {$a <=> $b} split(//, $i))}){
+ push @circular_primes, $i;
+ $circular{join("", sort {$a <=> $b} split(//, $i))} = -1;
+ }
+ $i++;
+ redo if @circular_primes < $n;
+ }
+ return @circular_primes;
+}
+
+sub first_10_circular_primes{
+ return first_n_circular_primes(10);
+}
+
+MAIN:{
+ print join(", ", first_10_circular_primes()) . "\n";
+} \ No newline at end of file
diff --git a/challenge-167/adam-russell/perl/ch-2.pl b/challenge-167/adam-russell/perl/ch-2.pl
new file mode 100644
index 0000000000..ba38e6c0dd
--- /dev/null
+++ b/challenge-167/adam-russell/perl/ch-2.pl
@@ -0,0 +1,40 @@
+use strict;
+use warnings;
+##
+# Implement a subroutine gamma() using the Lanczos approximation method.
+##
+use POSIX;
+use Math::Complex;
+
+use constant EPSILON => 1e-07;
+
+sub lanczos{
+ my($z) = @_;
+ my @p = (676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059, 12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7);
+ my $y;
+ $z = new Math::Complex($z);
+ if(Re($z) < 0.5){
+ $y = M_PI / (sin(M_PI * $z) * lanczos(1 - $z));
+ }
+ else{
+ $z -= 1;
+ my $x = 0.99999999999980993;
+ for my $i (0 .. @p - 1){
+ $x += ($p[$i] / ($z + $i + 1));
+ }
+ my $t = $z + @p - 0.5;
+ $y = sqrt(2 * M_PI) * $t ** ($z + 0.5) * exp(-1 * $t) * $x;
+ }
+ return Re($y) if abs(Im($y)) <= EPSILON;
+ return $y;
+}
+
+sub gamma{
+ return lanczos(@_);
+}
+
+MAIN:{
+ print gamma(3) . "\n";
+ print gamma(5) . "\n";
+ print gamma(7) . "\n";
+} \ No newline at end of file