[libmath-prime-util-perl] 65/72: Example for abundant, deficient, perfect numbers

Partha P. Mukherjee ppm-guest at moszumanska.debian.org
Thu May 21 18:49:42 UTC 2015


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

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

commit 4bb0db78d378ca62d1956dbabca9b45ebb30361a
Author: Dana Jacobsen <dana at acm.org>
Date:   Fri Oct 11 16:53:23 2013 -0700

    Example for abundant, deficient, perfect numbers
---
 examples/abundant.pl | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/examples/abundant.pl b/examples/abundant.pl
new file mode 100644
index 0000000..23b3564
--- /dev/null
+++ b/examples/abundant.pl
@@ -0,0 +1,38 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+# Find the first N abundant, deficient, or perfect numbers.
+
+use Math::Prime::Util qw/divisor_sum next_prime is_prime/;
+use Math::BigInt try => "GMP,Pari";
+
+my $count = shift || 20;
+my $type = lc(shift || 'abundant');
+
+my $p = 0;
+if ($type eq 'abundant') {
+  while ($count-- > 0) {
+    do { $p++ } while divisor_sum($p)-$p <= $p;
+    print "$p\n";
+  }
+} elsif ($type eq 'deficient') {
+  while ($count-- > 0) {
+    do { $p++ } while divisor_sum($p)-$p >= $p;
+    print "$p\n";
+  }
+} elsif ($type eq 'perfect') {
+  # We'll use the chain of work by Euclid, Ibn al-Haytham, Euler, and others.
+  # We just look for 2^(p-1)*(2^p-1) where 2^p-1 is prime.
+  # Basically we're just finding Mersenne primes.
+  # It's possible there are odd perfect numbers larger than 10^1500.
+  while ($count-- > 0) {
+    while (1) {
+      $p = next_prime($p);
+      last if is_prime(Math::BigInt->new(2)->bpow($p)->bdec);
+    }
+    print Math::BigInt->from_bin( '0b' . '1'x$p . '0'x($p-1) ), "\n";
+  }
+} else {
+  die "Unknown type: $type\n";
+}

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



More information about the Pkg-perl-cvs-commits mailing list