diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-05-30 10:29:05 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-05-30 10:29:05 +0100 |
| commit | 09aa417f411a4bad93f0dbd10a3d8a2b1b3af261 (patch) | |
| tree | 939ec2fe4f5d0b121e59fa52e450c4e66c84fc4b /challenge-167 | |
| parent | 95b68ec3530b952a89d3317f05b7da7cb6fd4145 (diff) | |
| download | perlweeklychallenge-club-09aa417f411a4bad93f0dbd10a3d8a2b1b3af261.tar.gz perlweeklychallenge-club-09aa417f411a4bad93f0dbd10a3d8a2b1b3af261.tar.bz2 perlweeklychallenge-club-09aa417f411a4bad93f0dbd10a3d8a2b1b3af261.zip | |
solutions
Diffstat (limited to 'challenge-167')
| -rw-r--r-- | challenge-167/james-smith/perl/ch-1.pl | 36 | ||||
| -rw-r--r-- | challenge-167/james-smith/perl/ch-2.pl | 27 |
2 files changed, 63 insertions, 0 deletions
diff --git a/challenge-167/james-smith/perl/ch-1.pl b/challenge-167/james-smith/perl/ch-1.pl new file mode 100644 index 0000000000..ba6a2a7599 --- /dev/null +++ b/challenge-167/james-smith/perl/ch-1.pl @@ -0,0 +1,36 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Test::More; +use Benchmark qw(cmpthese timethis); +use Data::Dumper qw(Dumper); +use Math::Prime::Util qw(next_prime is_prime); + +my($p,$N,@res) = (99,10); + +# Cannot raise $N above 10 as the 11th value is: +# 1,111,111,111,111,111,111 +# which is 5.5 trillion times larger than the 10th value... + +O: while( @res < $N ) { + ## Get the next prime - skip to next one if contains an even digit + ## As each digit is used as the last digit we know that this is not + ## a circular prime... (also skip 5 for a similar reason) + next O if ($p = next_prime $p)=~/[024568]/; + ## Split the string into consituent numbers... + my @q = split//, $p; + ## Check $p is the lowest of the permutations + ## Here @q starts out as the list of digits - but gets assigned the + ## list of rotations... This avoids us using a cache to see if we + ## have seen that combination of digits - slower but more memory + ## efficient... + ## If it is we then check to see if all are prime! + push @res, $p unless + ( grep { $p>$_ } @q = map { push @q, shift @q; join '',@q } 2..@q ) + || grep { !is_prime( $_ ) } @q; +} +say for @res; + diff --git a/challenge-167/james-smith/perl/ch-2.pl b/challenge-167/james-smith/perl/ch-2.pl new file mode 100644 index 0000000000..fd31b54b0d --- /dev/null +++ b/challenge-167/james-smith/perl/ch-2.pl @@ -0,0 +1,27 @@ +#!/usr/local/bin/perl + +use strict; + +use warnings; +use feature qw(say); +use Const::Fast qw(const); + +const my $PI => 3.1415926535897932384626433832; +const my $RP => 2.5066282746310002416123552393; +const my $EP => 0.000000000001; +const my $X => 0.99999999999980993; +const my @PV => ( + 676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059, + 12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7, +); + +say sprintf( '%5.2f - %30.4f', $_/2, gamma($_/2) ) =~ s{[.]0000$}{}r for -40..40; + +sub gamma { + my($i,$x,$z)=(0,$X,$_[0]); + return ($z<=0 && abs($z-int$z)<$EP) ? 'inf' + : $z < 0.5 ? $PI / sin($PI*$z) * gamma( 1-$z ) + : [map({$x+=$_/($z+$i++)}@PV),abs(($i=$RP*($i=$z+@PV-1.5)**($z-0.5)*exp(-$i)*$x)-int$i)<$EP?int$i:$i]->[-1] + ; +} + |
