This is an automated email from the git hooks/post-receive script.

ppm-guest pushed a commit to annotated tag v0.21
in repository libmath-prime-util-perl.

commit 5f02d79dcfa55a6ad37132eaa2c1fc155222bbb4
Author: Dana Jacobsen <d...@acm.org>
Date:   Sun Feb 10 02:34:13 2013 -0800

    Add circular primes to primes.pl (OEIS 68652, also in Project Euler problem 
35)
---
 TODO                              |  2 +-
 bin/primes.pl                     | 17 +++++++++++++++++
 examples/make-script-test-data.pl |  1 +
 3 files changed, 19 insertions(+), 1 deletion(-)

diff --git a/TODO b/TODO
index 0957ee4..de31039 100644
--- a/TODO
+++ b/TODO
@@ -45,4 +45,4 @@
   each), then use it to get 32-bit irands.  This would only be used if they
   didn't give us a RNG (so they don't care about strict crypto).
 
-- Add PE 35 Circular primes and PE 291 Panatoipal primes to bin/primes.pl
+- Add PE 291 Panatoipal primes to bin/primes.pl
diff --git a/bin/primes.pl b/bin/primes.pl
index 8ba5e7e..ae9e272 100755
--- a/bin/primes.pl
+++ b/bin/primes.pl
@@ -91,6 +91,7 @@ GetOptions(\%opts,
            'pnp1|A005234',
            'pnm1|A006794',
            'euclid|A018239',
+           'circular|A068652',
            'provable',
            'nompugmp',   # turn off MPU::GMP for debugging
            'version',
@@ -370,6 +371,18 @@ sub is_good_prime {
   1;
 }
 
+# Assumes the input is prime.  Returns 1 if all digit rotations are prime.
+sub is_circular_prime {
+  my $p = shift;
+  return 1 if $p < 10;
+  return 0 if $p =~ tr/024568//;
+  # TODO: BigInts
+  foreach my $rot (1 .. length($p)-1) {
+    return 0 unless is_prime( substr($p, $rot) . substr($p, 0, $rot) );
+  }
+  1;
+}
+
 sub merge_primes {
   my ($genref, $pref, $name, @primes) = @_;
   if (!defined $$genref) {
@@ -485,6 +498,9 @@ sub gen_and_filter {
   if (exists $opts{'pnp1'}) {
     @$p = grep { is_prime( primorial(Math::BigInt->new($_))+1 ) } @$p;
   }
+  if (exists $opts{'circular'}) {
+    @$p = grep { is_circular_prime($_) } @$p;
+  }
   if (exists $opts{'pillai'}) {
     @$p = grep { is_pillai($_); } @$p;
   }
@@ -579,6 +595,7 @@ to only those primes additionally meeting these conditions:
   --pnp1       Primorial+1      p#+1 is prime
   --pnm1       Primorial-1      p#-1 is prime
   --euclid     Euclid           pn#+1 is prime
+  --circular   Circular         all digit rotations of p are prime
   --provable                    Ensure all primes are provably prime
 
 Note that options can be combined, e.g. display only safe twin primes.
diff --git a/examples/make-script-test-data.pl 
b/examples/make-script-test-data.pl
index 760f4eb..9e60a0f 100755
--- a/examples/make-script-test-data.pl
+++ b/examples/make-script-test-data.pl
@@ -21,6 +21,7 @@ my @test_data = (
   [ 1359, "Twin",        "twin",       0],
   [ 5385, "Safe",        "safe",       0],
   [ 5384, "SG",          "sophie",     0],
+  [68652, "Circular",    "circular",   0],
   [ 2407, "Cuban y+1",   "cuban1",     0],
   [ 2648, "Cuban y+2",   "cuban2",     0],
   [ 2385, "Palindromic", "palin",      32_965_656_923],

-- 
Alioth's /usr/local/bin/git-commit-notice on 
/srv/git.debian.org/git/pkg-perl/packages/libmath-prime-util-perl.git

_______________________________________________
Pkg-perl-cvs-commits mailing list
Pkg-perl-cvs-commits@lists.alioth.debian.org
http://lists.alioth.debian.org/cgi-bin/mailman/listinfo/pkg-perl-cvs-commits

Reply via email to